Advertisement
Kulas_Code20

length and concatenate and compare string in c

Apr 13th, 2021
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. int main()
  4. {
  5.     char str1[50], str2[50];
  6.     int str1Cntr=0, str2Cntr=0;
  7.     int compare;
  8.    
  9.     printf("Enter a word1: ");
  10.     scanf("%s",str1);
  11.     printf("Enter a word2: ");
  12.     scanf("%s",str2);
  13.     //to get the legth of the strings
  14.     str1Cntr = strlen(str1);
  15.     str2Cntr = strlen(str2);
  16.    
  17.     //concatenate the two string using strcat
  18.     strcat(str1,str2);
  19.    
  20.     // comparing both the strings using strcmp() function
  21.     compare = strcmp(str1,str2);
  22.    
  23.     //display the legth of string 1 and 2
  24.     printf("\nThe length of the first string is: %d", str1Cntr);
  25.     printf("\nThe length of the second string is: %d", str2Cntr);
  26.     //display concatenate the two string
  27.     printf("\nConcatenated two String: %s\n", str1);
  28.     //display if the string is equal
  29.     if(compare==0) {
  30.         printf("strings are equal");  
  31.     }
  32.     else{  
  33.         printf("strings are not equal");
  34.     }
  35.    
  36.    
  37.     return 0;
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement