Write a C program to read two strings and explain string library function.
1)strlen()
2)strcyp()
3)strcat()
4) strcmp()
#include <stdio.h>
#include <string.h>
int main() {
char str1[20];
char str2[20];
printf("Enter string 1: ");
scanf("%[^\n]",str1);
fflush(stdin);
printf("Enter string 2: ");
scanf("%[^\n]",str2);
//1) strlen() - this function returns the length of the string
printf("The length of string 1: %d\n",strlen(str1));
printf("The length of string 2: %d\n",strlen(str2));
//2) strcyp() - this function allows to copy string from str1 to str 2
strcpy(str2, str1);
printf("Copy string from str1 to str2: %s\n",str2);
//3) strcat() - this function allows concatenate str1 and str2
strcat(str2, str1);
printf("Concatenate str1 and str2: %s\n",str2);
//4) strcmp() - this function allows compare two string str1 and str2. If the strings are equal, the function returns 0.
printf("strcmp(str1, str2) = %d\n", strcmp(str1, str2));
getchar();
getchar();
return 0;
}
Comments
Leave a comment