Advertisement
Siddiqui2583

Strcmp manually

Jul 28th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. int stringcmp();
  4. int main()
  5. {
  6.     char s1[100],s2[100];
  7.     gets(s1);
  8.     gets(s2);
  9.     int i=stringcmp(s1,s2);
  10.     if(i==1)
  11.         printf("1\n");
  12.     if(i==-1)
  13.         printf("-1\n");
  14.     if(i==0)
  15.         printf("0\n");
  16.  
  17.     return 0;
  18. }
  19. int stringcmp(char s1[100],char s2[100])
  20. {
  21.     int i;
  22.  
  23.     for(i=0;s1[i] != '\0' && s2[i] != '\0';i++){
  24.         if(s1[i]>s2[i])
  25.             return 1;
  26.         if(s1[i]<s2[i])
  27.             return -1;
  28.     }
  29.     if(strlen(s1[i])==strlen(s2[i]))
  30.         return 0;
  31.     if(strlen(s1[i])<strlen(s2[i]))
  32.         return -1;
  33.     if(strlen(s1[i])>strlen(s2[i]))
  34.         return 1;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement