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
S | A | i | \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”;
O | m | S | a | i | \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