P068.c (799B)
1 /* Write a c program that perform the operation of strcmp() */ 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 int string_compare1(char s1[], char s2[]) 7 { 8 int i, c, cmp = 0, p = 0; 9 i = 0; 10 while (s1[i] != '\0' && s2[i] != '\0') 11 { 12 if (s1[i] != s2[i]) 13 { 14 cmp = s1[i] - s2[i]; 15 p = 1; 16 break; 17 } 18 i++; 19 } 20 c = s1[i] != '\0' ? s1[i] : (-1) * s2[i]; 21 return p == 1 ? cmp : c; 22 } 23 int string_compare2(char *s1, char *s2) 24 { 25 while (*s1 != '\0' && (*s1 == *s2)) 26 { 27 s1++; 28 s2++; 29 } 30 return *s1 - *s2; 31 } 32 33 int main() 34 { 35 char str1[] = "AMIT"; 36 char str2[] = "Amit"; 37 printf("Result 1 = %d", string_compare1(str1, str2)); 38 printf("\nResult 2 = %d", string_compare2(str1, str2)); 39 return 0; 40 }