String Comparison in C

String comparison in C is crucial in applications like password verification and plagiarism detection. It involves methods like using the strcmp() function, manual comparisons, pointers, and recursion. Understanding how strcmp() works and its alternatives is key to efficiently comparing strings and determining their lexicographical order.
Understanding String Comparison of Two Strings in C
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.
-
String comparison by using strcmp() String Library function.
-
String comparison without using strcmp() function .
-
String comparison by using pointers.
-
String comparison by using recursion.
String comparison by Using String Library Function
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.
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
Syntax of the strcmp() Function
In the syntax above, the function prototype is int strcmp(const char *str1, const char *str2);, where the parameters are const char pointers named str1 and str2. Note that strcmp is case-sensitive, so "Apple" and "apple" are not equal.
| Return Value | Description |
|---|---|
| 0 | Returns 0, When both strings are exactly the same. |
| <0 | The function will return a negative number if the ASCII value of a character in the first string is smaller than the ASCII value of a character in the second string. |
| >0 | The function will return a positive value if a character's ASCII value in the first string is bigger than a character's ASCII value in the second string. |
Example
Output:
This compares two strings character by character using character arrays. If input is read with fgets(), remove the trailing newline before comparing, or the result may be unexpected.
You can run and check your code in this Online C Compiler
Analysis of the Program
-
We’ve declared two character arrays, str1, and str2, respectively, then take input for them; in C, strings are stored as arrays terminated by the null character.
-
The strcmp() function is used to compare strings (str1, str2). The strings str1 and str2 will be compared using this function character by character until they are identical up to a difference or the end. If the function returns a value 0, it signifies that the strings are equal otherwise, strings are not equal.
-
The same comparison logic is also used when sorting an array of strings in lexicographical order based on ASCII values.
String Comparison without Using strcmp() Function
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.
Output:
-
In the code example, We've declared two char arrays and are taking user input as strings.
-
We've built a compareTwoString() function that takes two user input strings as an argument and compares character by character using a while loop. If the function returns 0, then the strings are equal otherwise, strings are not equal.
String Comparison by Using Pointers
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.
Output:
Explanation:
-
In the code example, We've declared two char arrays str1 ,str2 and then take input for them.
-
A compareTwoString() function has been defined, which takes two char pointers as an argument. The address of str1 is held by the 'a' pointer, whereas the address of str2 is held by the 'b' pointer. We've constructed a while loop inside the function that will run until the pointer a or b does not reach a null character.
Using Recursion
We can use recursion to compare two strings, and this example helps reinforce core concepts of string comparison in the C programming language, so we’ll calculate the lengths of both strings and the compareTwoString function will keep calling itself until the condition becomes false.
Output:
Explanation:
-
In the code example, We’ve declared two char arrays str1 ,str2 and take input for them.
-
A compareTwoString() calculates the length of both string using strlen() function.
-
The compareTwoString() function will return 0 if the lengths of both strings are not equal.
-
If the lengths of both strings are equal then we will compare str1[i] with str2[i], and if the loop uses int i, str1[i] equals str2[i], we will increase the value of c and i. then compareTwoString(str1, str2) keeps calling itself until i < l1 is false.
-
compareTwoString() function gives 1 if c is equal to i as c counts the number of equal characters at the same index. If c becomes equal to i this specify strings are equal.
-
compareTwoString() function returns 0 if strings are not equal otherwise 1 if strings are equal.
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Conclusion
-
A string is a group of characters, and direct comparison operators do not compare string contents in C.
-
We can compare two strings in C using a variety of approaches.
-
The two strings to be checked must be compared character by character.
-
The c standard library provides strcmp() for standard comparisons and returns 0 if two strings are equal.
-
The standard library does not define case-insensitive comparison across platforms; strcasecmp compares strings while ignoring case differences in POSIX/Linux environments, while _stricmp is used in Windows environments.
-
We can compare two strings without string library function also using loops.
-
We can also compare two strings using pointers or recursion.