In C programming, strings are fundamental for handling text and character sequences. Comparing strings is a common operation, whether you’re sorting data, verifying user input, or performing text-based searches. The C standard library provides a handy function, strcmp()
, located in the <string.h>
header file, that allows you to compare two strings lexicographically. This article will delve into the strcmp()
function, explaining its syntax, how it works, and illustrating its usage with practical examples to help you effectively Compare Strings In C.
Understanding the strcmp()
Function
The strcmp()
function is designed to compare two strings, often referred to as string1
and string2
, and determine their lexicographical order. Lexicographical order is essentially dictionary order. It compares strings character by character based on the ASCII values of the characters.
Syntax and Parameters
The syntax for the strcmp()
function is straightforward:
#include <string.h>
int strcmp(const char *string1, const char *string2);
Parameters:
string1
: A pointer to the first string you want to compare. It’s treated as a null-terminated C-style string.string2
: A pointer to the second string for comparison, also a null-terminated C-style string.
Both parameters are of type const char *
, indicating that the function expects pointers to character arrays and that it will not modify the original strings.
Return Values Explained
The strcmp()
function returns an integer value that signifies the relationship between the two strings being compared:
0
(Zero): Returned whenstring1
andstring2
are identical. This means they have the same characters in the same order.> 0
(Positive Value): Returned whenstring1
is lexicographically greater thanstring2
. This occurs when the first differing character instring1
has a higher ASCII value than the corresponding character instring2
, or ifstring1
is longer andstring2
is a prefix ofstring1
.< 0
(Negative Value): Returned whenstring1
is lexicographically less thanstring2
. This happens when the first differing character instring1
has a lower ASCII value than the corresponding character instring2
, or ifstring2
is longer andstring1
is a prefix ofstring2
.
How strcmp()
Works: Lexicographical Comparison
The strcmp()
function operates by comparing the strings character by character, starting from the first character of each string. It proceeds as follows:
- Character-by-Character Comparison:
strcmp()
begins by comparing the ASCII values of the first characters ofstring1
andstring2
. - Equality Check and Iteration: If the characters are equal,
strcmp()
moves to the next character in both strings and repeats the comparison. This process continues until one of the following conditions is met:- Characters are Unequal: If
strcmp()
encounters a pair of characters that are different, it determines which string is lexicographically greater based on their ASCII values. - Null Terminator is Reached: If
strcmp()
reaches the null terminator () in both strings simultaneously, it means the strings are identical, and it returns
0
. - Null Terminator in One String First: If a null terminator is encountered in one string before a mismatch is found, the shorter string (the one with the null terminator first) is considered lexicographically smaller if it’s a prefix of the longer string. If the strings are identical up to the null terminator of the shorter string, then the shorter string is considered lexicographically smaller.
- Characters are Unequal: If
- Determining Lexicographical Order:
- If a mismatch is found,
strcmp()
calculates the difference between the ASCII values of the differing characters. This difference (positive or negative) determines the return value, indicating which string comes first lexicographically. - If the null terminators are reached at the same time without any mismatches, the function returns
0
, signifying equality.
- If a mismatch is found,
Let’s illustrate with examples:
- Comparing “apple” and “apple”:
strcmp("apple", "apple")
returns0
(identical). - Comparing “apple” and “banana”:
strcmp("apple", "banana")
returns a negative value because ‘a’ (from “apple”) has a lower ASCII value than ‘b’ (from “banana”). - Comparing “zebra” and “apple”:
strcmp("zebra", "apple")
returns a positive value because ‘z’ (from “zebra”) has a higher ASCII value than ‘a’ (from “apple”). - Comparing “car” and “carpet”:
strcmp("car", "carpet")
returns a negative value because “car” is a prefix of “carpet”. - Comparing “carpet” and “car”:
strcmp("carpet", "car")
returns a positive value because “carpet” is lexicographically greater than “car”.
Practical Examples of strcmp()
in C
Let’s examine some C code examples to see strcmp()
in action.
Comparing Identical Strings
This example demonstrates comparing two 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 equaln");
} else {
printf("Strings are not equaln");
}
return 0;
}
Output:
Strings are equal
Explanation: strcmp(s1, s2)
returns 0
because s1
and s2
both contain the string “Hello”. The if
condition evaluates to true, and “Strings are equal” is printed.
Comparing Lexicographically Different Strings
This example shows how strcmp()
behaves when comparing strings that are lexicographically different.
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "coding";
char str2[] = "comparison";
int res = strcmp(str1, str2);
if (res == 0) {
printf("Strings are equaln");
} else if (res < 0) {
printf(""%s" is lexicographically less than "%s"n", str1, str2);
} else {
printf(""%s" is lexicographically greater than "%s"n", str1, str2);
}
return 0;
}
Output:
"coding" is lexicographically greater than "comparison"
Explanation: strcmp(str1, str2)
compares “coding” and “comparison”. The first differing character is ‘o’ in “coding” and ‘m’ in “comparison”. Since ‘o’ has a higher ASCII value than ‘m’, strcmp()
returns a positive value. The else if (res < 0)
condition is false, and the else
block is executed, indicating that “coding” is lexicographically greater.
Using strcmp()
to Sort Strings
A powerful application of strcmp()
is in sorting arrays of strings. You can use it as a comparison function with sorting algorithms like qsort()
(quicksort) from the <stdlib.h>
library.
#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 *stringArray[] = {"Mango", "Apple", "Grapes", "Banana"};
int n = sizeof(stringArray) / sizeof(stringArray[0]);
qsort(stringArray, n, sizeof(stringArray[0]), compareStrings);
printf("Sorted strings are:n");
for (int i = 0; i < n; i++) {
printf("%sn", stringArray[i]);
}
return 0;
}
Output:
Sorted strings are:
Apple
Banana
Grapes
Mango
Explanation:
compareStrings
Function: This function is designed to be used withqsort()
. It takes twovoid
pointers, casts them toconst char **
(pointers to string pointers), and then usesstrcmp()
to compare the strings they point to.qsort()
Function:qsort()
is a generic sorting function.stringArray
: The array of strings to be sorted.n
: The number of elements in the array.sizeof(stringArray[0])
: The size of each element (which is achar *
pointer).compareStrings
: The comparison function we defined.
qsort()
uses compareStrings
to determine the order of elements, effectively sorting the stringArray
lexicographically. The loop then prints the sorted strings.
Advantages and Considerations when Using strcmp()
Advantages:
- Standard Library Function:
strcmp()
is part of the C standard library, making it readily available in any C environment. - Efficiency: It’s generally efficient for comparing C-style strings. The comparison stops as soon as a difference is found or the end of the strings is reached.
- Clear Return Values: The return values (
0
, positive, negative) provide a clear and standardized way to interpret the comparison result.
Considerations:
- Case Sensitivity:
strcmp()
is case-sensitive. “Apple” and “apple” are considered different. If you need case-insensitive comparisons, consider using functions likestrcasecmp()
(if available on your system, often in POSIX systems) or converting strings to a consistent case before comparison. - Locale-Specific Comparisons:
strcmp()
performs comparisons based on ASCII values, which might not align with locale-specific sorting rules in all languages. For locale-aware string comparisons, functions likestrcoll()
might be more appropriate. - Binary vs. Text Comparison:
strcmp()
is designed for text comparison. If you need to compare raw memory blocks (which might include strings but are not necessarily null-terminated C-strings), functions likememcmp()
should be used.
FAQs about strcmp()
Q: When does strcmp()
return zero?
A: strcmp()
returns zero when the two strings being compared are exactly identical.
Q: What does a positive return value from strcmp()
signify?
A: A positive return value indicates that the first string is lexicographically greater than the second string.
Q: What does a negative return value from strcmp()
signify?
A: A negative return value indicates that the first string is lexicographically less than the second string.
Q: Can strcmp()
be used to compare non-string data types?
A: No, strcmp()
is specifically designed for comparing null-terminated C-style strings (char *
). It should not be used to directly compare other data types. For comparing memory blocks of arbitrary data, use memcmp()
.
Conclusion
The strcmp()
function is an essential tool in C programming for comparing strings. Understanding its syntax, return values, and how it performs lexicographical comparisons is crucial for tasks involving string manipulation, sorting, and data validation. By using strcmp()
effectively, you can confidently implement string comparisons in your C programs. Remember to consider case sensitivity and locale-specific requirements for more complex string handling scenarios.