In the realm of C programming, strings are fundamental data types used to represent text. Comparing strings is a common operation in many applications, from sorting lists of names to validating user input. C provides a built-in function, strcmp()
, within its string library to efficiently compare two strings lexicographically. This article delves into the intricacies of strcmp()
, providing a comprehensive guide for both novice and experienced C programmers looking to master string comparison in C.
Understanding strcmp()
Function in C
The strcmp()
function in C is a powerful tool for comparing two strings. It determines the lexicographical relationship between them, essentially checking if they are identical, or if one comes before or after the other in dictionary order. This comparison is based on the ASCII values of the characters in the strings.
Syntax of strcmp()
To utilize the strcmp()
function, you need to include the <string.h>
header file in your C program. The syntax is straightforward:
strcmp(s1, s2);
Parameters:
s1
: A pointer to the first string (character array).s2
: A pointer to the second string (character array).
Return Value:
The strcmp()
function returns an integer value that indicates the relationship between the two strings:
- 0 (Zero): Returned if the strings
s1
ands2
are identical. This means they have the same sequence of characters. - Greater than 0: Returned if
s1
is lexicographically greater thans2
. This implies that in dictionary order,s1
would come afters2
. - Less than 0: Returned if
s1
is lexicographically less thans2
. This means in dictionary order,s1
would come befores2
.
How strcmp()
Lexicographically Compares Strings
The magic of strcmp()
lies in its lexicographical comparison. Here’s a breakdown of how it works step-by-step:
-
Character-by-Character Comparison:
strcmp()
starts by comparing the first character of both strings (s1
ands2
). It compares their ASCII values. -
Equality Check and Iteration: If the characters are equal,
strcmp()
proceeds to compare the next characters in both strings. This process continues character by character. -
Non-Matching Character Found: If
strcmp()
encounters characters at the same position that are different:- It determines which character has a higher ASCII value.
- If the character in
s1
has a higher ASCII value,strcmp()
returns a positive value (> 0). - If the character in
s1
has a lower ASCII value,strcmp()
returns a negative value (< 0).
-
Null Terminator Encountered: If
strcmp()
reaches the end of one or both strings (indicated by the null terminator) before finding any mismatched characters:
- If both strings end at the same position, it means they are identical, and
strcmp()
returns 0. - If one string is a prefix of the other (e.g., “Geek” and “Geeks”), the shorter string is considered lexicographically smaller.
- If both strings end at the same position, it means they are identical, and
Let’s visualize this with images to solidify your understanding.
In the scenario above, strcmp()
compares “Geeks” and “Geeks”. Each character matches until the null terminator is reached in both strings simultaneously. Therefore, strcmp()
correctly identifies them as equal and returns 0.
Here, “apple” is compared to “banana”. The comparison begins with ‘a’ and ‘b’. Since ‘a’ has a smaller ASCII value than ‘b’, strcmp()
immediately determines that “apple” is lexicographically smaller than “banana” and returns a negative value.
In this example, “zebra” is compared with “apple”. ‘z’ has a larger ASCII value than ‘a’. strcmp()
identifies “zebra” as lexicographically greater than “apple” and returns a positive value.
Practical Examples of strcmp()
in C
Let’s explore various code examples to demonstrate the practical applications of strcmp()
.
Example 1: Checking for Identical Strings
#include <stdio.h>
#include <string.h>
int main() {
char s1[] = "Hello";
char s2[] = "Hello";
int result = strcmp(s1, s2);
if (result == 0) {
printf("Strings are identicaln");
} else {
printf("Strings are not identicaln");
}
return 0;
}
Output:
Strings are identical
Explanation: In this example, strcmp()
compares “Hello” and “Hello”. As they are exactly the same, strcmp()
returns 0, and the program correctly prints “Strings are identical”.
Example 2: Finding Lexicographically Greater String
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "orange";
char str2[] = "apple";
int result = strcmp(str1, str2);
if (result > 0) {
printf(""%s" is lexicographically greater than "%s"n", str1, str2);
} else {
printf(""%s" is not lexicographically greater than "%s"n", str1, str2);
}
return 0;
}
Output:
"orange" is lexicographically greater than "apple"
Explanation: Here, strcmp()
compares “orange” and “apple”. ‘o’ has a higher ASCII value than ‘a’. Therefore, strcmp()
returns a positive value, indicating “orange” comes after “apple” lexicographically.
Example 3: Finding Lexicographically Smaller String
#include <stdio.h>
#include <string.h>
int main() {
char string1[] = "cat";
char string2[] = "dog";
int result = strcmp(string1, string2);
if (result < 0) {
printf(""%s" is lexicographically smaller than "%s"n", string1, string2);
} else {
printf(""%s" is not lexicographically smaller than "%s"n", string1, string2);
}
return 0;
}
Output:
"cat" is lexicographically smaller than "dog"
Explanation: In this case, strcmp()
compares “cat” and “dog”. ‘c’ has a lower ASCII value than ‘d’. Consequently, strcmp()
returns a negative value, confirming that “cat” is lexicographically smaller than “dog”.
Example 4: Sorting an Array of Strings using strcmp()
strcmp()
is exceptionally useful as a comparison function for sorting strings. Combined with the qsort()
function from <stdlib.h>
, you can easily sort an array of strings.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int compareStrings(const void *a, const void *b) {
return strcmp(*(const char **)a, *(const char **)b);
}
int main() {
const char *names[] = {"Charlie", "Alice", "Bob", "David"};
int n = sizeof(names) / sizeof(names[0]);
qsort(names, n, sizeof(names[0]), compareStrings);
printf("Sorted names:n");
for (int i = 0; i < n; i++) {
printf("%sn", names[i]);
}
return 0;
}
Output:
Sorted names:
Alice
Bob
Charlie
David
Explanation:
compareStrings
function: This function acts as the comparator forqsort()
. It takes two void pointers, casts them toconst char **
(pointers to string pointers), and usesstrcmp()
to compare the strings they point to.qsort()
function: Sorts thenames
array using thecompareStrings
function to determine the order.- The output shows the names array sorted lexicographically using
strcmp()
as the underlying comparison mechanism.
FAQs about C strcmp()
Q1: When does strcmp()
return zero?
strcmp()
returns zero if and only if the two strings being compared are exactly identical.
Q2: What does a positive return value from strcmp()
signify?
A positive return value from
strcmp()
indicates that the first string is lexicographically greater than the second string.
Q3: What does a negative return value from strcmp()
mean?
A negative return value from
strcmp()
signifies that the first string is lexicographically smaller than the second string.
Q4: Can strcmp()
be used to compare non-string data types in C?
No,
strcmp()
is specifically designed for comparing null-terminated strings (character arrays) in C. It cannot be used to directly compare other data types like integers, floats, or structures. For comparing other data types, you would use standard comparison operators (e.g.,==
,<
,>
).
Conclusion
The strcmp()
function is an essential component of C’s string manipulation capabilities. Understanding its lexicographical comparison mechanism, syntax, and return values is crucial for any C programmer working with strings. From simple string equality checks to complex sorting algorithms, strcmp()
provides a robust and efficient way to compare strings in C. Mastering strcmp()
will undoubtedly enhance your ability to write effective and reliable C programs that process textual data.