C program to compare two string with using pointer.












C program to compare two string using pointer.




C program to compare two string pointer.



#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 strings are equal."); } else if(result < 0) { printf("\n First string is lexicographically smaller than second."); } else { printf("\n First string is lexicographically greater than second."); } return 0; } /** * Compares two strings lexicographically. */ int compare(char * text1, char * text2) { while((*text1 && *text2) && (*text1 == *text2)) { text1++; text2++; } return *text1 - *text2; }


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.