Explain strlen() and strcat() with syntax and example in c
Answers
Answer:
In C programming, strcat() concatenates (joins) two strings.
The strcat() function is defined in <string.h> header file.
It takes two arguments, i.e, two strings or character arrays, and stores the resultant concatenated string in the first string specified in the argument.
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "This is ", str2[] = "programiz.com";
//concatenates str1 and str2 and resultant string is stored in str1.
strcat(str1,str2);
puts(str1);
puts(str2);
return 0;
}
strlen()
The function takes a single argument, i.e, the string variable whose length is to be found, and returns the length of the string passed.
The strlen() function is defined in <string.h> header file.
#include <stdio.h>
#include <string.h>
int main()
{
char a[20]="Program";
char b[20]={'P','r','o','g','r','a','m','\0'};
char c[20];
printf("Enter string: ");
gets(c);
printf("Length of string a = %d \n",strlen(a));
//calculates the length of string before null charcter.
printf("Length of string b = %d \n",strlen(b));
printf("Length of string c = %d \n",strlen(c));
return 0;
}