Learn about String Comparison in C. Show In this article, we are going to discuss string comparison in C. We can compare two strings in C using a variety of approaches, including the string library function strcmp(), without a function, pointers, and recursion. Ever wondered how the websites check if the passwords match when you sign up or how the software detects if there’s any plagiarism, or how the spam filtering in your mail works? There is one solution to all the above things - String Comparison. Comparing two strings or string comparison in C involves finding out if they are the same or not. This is done by using some built-in function or comparing the strings character by character to determine if they are equal. In case they are not equal, we can also analyze and find out which string is lexicographically (lexicographic order means dictionary order, that is, the words which start from 'a' come before the words that start with 'b' and the earlier is lexicographically smaller than the later, we'll see about this later in the article) larger than the other by the various methods of string comparison in C. There are four methods for string comparison in C.
The string library functions are pre-defined in string.h header file used to do operations on the strings. strcmp() function is used to compare two strings. The strcmp() function takes two strings as input and returns an integer result that can be zero, positive, or negative. The strcmp() function compares both strings characters. If both strings have the same character at the same index till all of the characters have been compared or the pointer reaches the null character '\0' in both strings then we can say that both strings are equal. Syntax of the strcmp() Functionint strcmp (const char* str1, const char* str2); In the syntax above, two arguments, str1 and str2, are passed as strings and the return type is int, indicating that strcmp() gives an integer value. Possible Return Values from the strcmp() Function
Example# <stdio.h> #<string.h> int main() { char str1[50]; // declaration of char array char str2[50]; // declaration of char array int value; // declaration of integer variable printf("Enter the first string : "); scanf("%s",str1); printf("Enter the second string : "); scanf("%s",str2); // comparing both the strings using strcmp() function value = strcmp(str1,str2); if(value == 0) printf("strings are same"); else printf("strings are not same"); return 0; } Output: Enter the first string : scaler Enter the second string : interviewbit strings are not same Analysis of the Program
String comparison in C is also possible by using without strcmp() function. We could compare the two strings by traversing each index using a loop and comparing each character one by one. # <stdio.h> int compareTwoString(char[],char[]); int main() { char str1[50]; // declaration of char array char str2[50]; // declaration of char array printf("Enter the first string : "); scanf("%s",str1); printf("Enter the second string : "); scanf("%s",str2); int c= compareTwoString(str1,str2); // calling compareTwoString() function if(c==0) printf("strings are same"); else printf("strings are not same"); return 0; } // Comparing both the strings. int compareTwoString(char a[],char b[]) { int flag=0,i=0; // integer variables declaration while(a[i]!='\0' &&b[i]!='\0') // while loop { if(a[i]!=b[i]) { flag=1; break; } i++; } if(a[i]!='\0'||b[i]!='\0') return 1; if(flag==0) return 0; else return 1; } Output: Enter the first string : coding Enter the second string : debugging strings are not same Explanation:
String comparison in C also possible by using pointers. In this approach, we use pointers to traverse the strings and then compare the characters pointed by the pointer. #<stdio.h> int compareTwoString(char *, char *); int main() { char str1[50]; // declaration of char array char str2[50]; // declaration of char array printf("Enter the first string : "); scanf("%s", str1); printf("\nEnter the second string : "); scanf("%s", str2); int compare = compareTwoString(str1, str2); // calling compareTwoString() function. if (compare == 0) printf("strings are equal"); else printf("strings are not equal"); return 0; } // Comparing both the strings using pointers int compareTwoString(char *a, char *b) { int flag = 0; while (*a != '\0' && *b != '\0') // while loop { if (*a != *b) { flag = 1; } a++; b++; } if(*a!='\0'||*b!='\0') return 1; if (flag == 0) return 0; else return 1; } Output: Enter the first string : Scaler Enter the second string : Scaler strings are equal Explanation:
We can use recursion to compare two strings, so we'll calculate the lengths of both strings and the compareTwoString function will keep calling itself until the condition becomes false. # <stdio.h> # <string.h> int compareTwoString(char str1[], char str2[]) { static int i = 0; // store the current index we are at in string // calculating the length of str1 int l1 = strlen(str1); //calculating the length of str2 int l2 = strlen(str2); static int c = 0; // store number of equal characters at same index // if the length of both strings are not equal then it will return as 0 if (l1 != l2) { return 0; } // if value of i is less than length of str1 // then their are still more characters left to compare else if (i < l1) { if (str1[i] == str2[i]) c++; // count number of equal characeter at same index i++; compareTwoString(str1, str2); } if (c == i) { return 1; } return 0; } int main() { // taking two input as char array char str1[50], str2[50], c; printf("Enter first string: "); //taking input for str1 fgets(str1, 50, stdin); printf("Enter second string: "); //taking input for str2 fgets(str2, 50, stdin); c = compareTwoString(str1, str2); if (c) { //this statement will be printed if both strings are equal printf("strings are equal"); } else { //this statement will be printed if both strings are not equal printf("strings are not equal"); } return 0; } Output: Enter first string: code Enter second string: code strings are equal Explanation:
|