×

strcat() Function in C

Strings in c:

A string is defined as the set of characters that are enclosed within the double quotations (" "). The String always ends with the null character ("\0"). The null character ("\0") indicates that it is the end of the string. It can also be enclosed with single quotes in c programming.

Syntax:

char variable [size] = "String data";

Example:

We can declare the string in c programming as two types

  • String [5] = {"s", "a", "I", "r", "\0"};
  • String [3] = "Sai";

It will be stored as

SAi\0

String Declaration:

S[0]S[1]S[2]S[3]S[4]S[5]

Above is a string of length 6. So we can store 5 characters and one null character at the last position by default.

String Initialisation:

We can declare or initialise a string in the below ways

Char t[] = “Vijay”;

Char t[] = {‘a’, ’e’, ’I’, ‘o’, ‘u’, ‘\0’};

Char t[5] = {‘a’, ’e’, ’I’, ‘o’, ‘\0’};

Char t [6] = “Om Sai”;

OmSai\0

How to Read a String:

We can read a string using the scanf () function. We will use “%s” for string datatype to read or print the string values.

Example:

//simple program to scan and print string
#include < stdio.h >
#include < conio.h >
int main()
{
char a[10];   //string declaration
clrscr();
printf(“enter the string value: “);
scanf(“%s”, a);       //here we take the string data from the user
printf(“the name is %s.”, a);       //we print the string data
return 0;
}

Output:

Enter the string value: Vijay
The name is Vijay.

String Handling Functions:

We can work more effectively with strings using pre-defined functions known as String handling functions. If we want to use string handling functions in our program, then we must include <string.h> pre-processer. Some of them are

  • Strlen () Function:

The string length function will print or compute the length of the given string.

Syntax: 

strlen(a);

Example:

//program for string length function
#include < stdio.h >
#include < string.h >
#include < conio.h >
int main()
{
int len;   //integer variable declaration
char a[] = {‘v’, ‘I’, ‘j’, ‘a’, ‘y’, ‘\0’};   //string declaration with data
clrscr();
len = strlrn(a);    //finding the length of string a and storing in len varaible
printf(“The size of the given string is %d”, len);  //printing the len of string
return 0;
}

Output:

The size of the given string is 6.
  • strcpy () Function:

The string copy function is used to copy the whole string or the content of one string to another.

Syntax: 

strcpy(a, b);

Example:

//program for string copy function
#include < stdio.h >
#include < string.h >
#include < conio.h >
int main()
{
char c[];     //string declaration
char a[] = “hi”;   //string declaration with data
char b[] = “all”;  //string declaration with data
clrscr();
strcpy (a, c);     //coping one string to another variable
printf(“The copied string is %s”, c);   //printing the copied string
return 0;
}

Output:

The copied string is hi.
  • Strcmp () Function:

The string compare function is used to compare two strings.

Syntax: 

strcmp(a, b);

Example:

//program for string concatenate function
#include < stdio.h >
#include < string.h >
#include < conio.h >
int main()
{
char c[];      //string declaration
char a[] = “Hi ”;   //string declaration with data
char b[] = “all”;   //string declaration with data
clrscr();
c = strcmp(a, b);   //comparing two strings
if(c == 0)
{
printf(“both strings are same”);  //prints if the given condition is true
}
else
{
printf(“both strings are not the same”);  //prints if the given condition is false
return 0;
}

Output:

Both strings are not the same.
  • Strcat () Function:

The string concatenate function is used to join or add or concatenate two strings.

Syntax: 

strcat(a, b);

Example:

//program for string concatenate function
#include < stdio.h >
#include < string.h >
#include < conio.h >
int main()
{
char c[];        //string declaration
char a[] = “Hi ”;   //string declaration with data
char b[] = “all”;    //string declaration with data
clrscr();
c = strcat (a, b);       //concatenating two strings and storing it in c variable
printf(“The concatenated string is %s”, c);  //printing the concatenated string
return 0;
}

Output:

The concatenated string is Hi all

Related Topics

Execution flow of C program

There are various steps of the execution of the C program that is given below. The following step of the execution Write source codes Preprocess Compile Link edit Load Execute Editor or...

1 minute read.

strtok() and strtok_r() functions in C with examples

strtok() and strtok_r() functions in C with examples C offer various strtok() and strtok r() methods for a string to be separated by a certain delimiter. Dividing a string is a...

2 minutes read.

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 minutes read.

tolower() Function in C

The tolower() capability is characterized in the ctype.h header document. In the event that the person passed is a capitalized letter set, the tolower() capability switches capitalized letters in order...

3 minutes read.

Format Specifier in C

Format Specifier in C Format specifiers can be described as an operator that is used to print the data referred by any object or variable in combination with the printf ()...

3 minutes read.

OpenGL in C

OpenGL stands for Open Graphics Library. It is a low-level, widely supported modeling and rendering software package that is widely available across all the platforms. It is an API used...

4 minutes read.

Run Time Polymorphism in C

What is polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. An individual who can have...

4 minutes read.

Snake Game in C

Snake game in C is basic desktop console program which was created in 1970’s. It is an old classic game which was played by every child across the world. This...

4 minutes read.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

3 minutes read.

String literals in C

Constants refer to the fixed values of the program that may not alter during the compiling ad execution. These fixed values are known as literals. In other words, the values...

3 minutes read.

Bit Stuffing Program in C

What is a Bit Stuffing? The process of inserting one or more extra bits (0) into data is known as bit stuffing. To provide signalling information to a recipient, these are...

3 minutes read.

Difference between rand() and srand() function in C

What is the rand()? The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.

How to use floor() function in C

Introduction: The floor() used as a function in the C programming language. This function uses to return the largest or most significant integer value. The integer value is smaller than...

3 minutes read.

Prime Number code in C

Prime Number Programs in C language In this tutorial, we will learn how to check whether the given number is a prime number or not, and how to print all the...

4 minutes read.

Formatted Input and output function in C

Formatted I/O functions display multiple outputs to the user by taking various inputs. All data types like int, float, char and double are supported by the formatted I/O function. In...

4 minutes read.

Function in C

Function is a group of statements that are used to perform any task. In other words, we can say that a function is a self- contained a block of programs...

3 minutes read.

Simple Programs in C Language

In this article, we will discuss the basic programs in C language. Addition of two values in C. Swapping two values. Finding the odd or even values. Finding vowels and consonants in C. Finding the...

11 minutes read.

C Pointers

Pointers in C A pointer is a variable, which contains the address of another variable, i.e., it’s a variable which has the address of another variable as its value. The utilization...

4 minutes read.

Top Array Keywords in C

Sorting array in C Types of array in C Array of structure in C How do you initialize an array in C Array declaration in C Array of strings in C Multidimensional array in C Reverse array...

1 minute read.

C program to compare the two strings

C program to compare the two strings Strings can be compared either by using the string function or without using string function. First, we will look at how we can compare the strings with...

4 minutes read.