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