C program to compare two string without using string library functions and with using loop.












C program to compare two string without using string library functions and with using loop.




C program to compare two string without using string library functions and with using loop.



#include <stdio.h> #define MAX 100 int compare(char * text1, char * text2); int main() { char text1[MAX], text2[MAX]; int result; printf("\n Enter first string: "); gets(text1); printf("\n Enter second string: "); gets(text2); result = compare(text1, text2); if(result == 0) { printf("\n Both or two strings are equal."); } else if(result < 0) { printf("First string is lexicographically smaller than second."); } else { printf("First string is lexicographically greater than second."); } return 0; } int compare(char * text1, char * text2) { int i = 0; while(text1[i] == text2[i]) { if(text1[i] == '\0' && text2[i] == '\0') break; i++; } return text1[i] - text2[i]; }


Output:
Enter first string: computer Enter second string: computer Both or two strings are equal. Enter first string: computer Enter second string: Computer First string is lexicographically greater than second. Enter first string: computer Enter second string: Computer First string is lexicographically greater than second.




Exercise: 1
C program to compare two strings.