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 the <string.h>
header file, specifically designed for lexicographically comparing two C-style strings (character arrays terminated by a null character). This article will provide an in-depth exploration of the strcmp()
function, covering its syntax, functionality, working mechanism, and practical examples to help you effectively utilize C String Compare operations in your C programs.
Understanding the Syntax of strcmp()
The strcmp()
function follows a straightforward syntax:
#include <string.h>
int strcmp(const char *str1, const char *str2);
Parameters:
str1
: A pointer to the first C-style string to be compared. It is of typeconst char *
, indicating that the function will not modify the string pointed to by this pointer.str2
: A pointer to the second C-style string to be compared, also of typeconst char *
.
Return Value:
The strcmp()
function returns an integer value based on the lexicographical comparison of str1
and str2
:
- 0 (Zero): Returned if
str1
andstr2
are identical. This means that both strings have the same sequence of characters. - Greater than 0 (Positive): Returned if
str1
is lexicographically greater thanstr2
. This indicates thatstr1
would come afterstr2
in dictionary order. - Less than 0 (Negative): Returned if
str1
is lexicographically less thanstr2
. This indicates thatstr1
would come beforestr2
in dictionary order.
How strcmp()
Function Works: A Lexicographical Approach
The strcmp()
function performs a lexicographical comparison, which is essentially dictionary order comparison. It compares the two input strings character by character based on their ASCII values. The process unfolds as follows:
-
Character-by-Character Comparison:
strcmp()
starts by comparing the first character ofstr1
with the first character ofstr2
. -
ASCII Value Evaluation: It compares the ASCII values of these characters.
-
Continuing Comparison:
- If the characters are the same,
strcmp()
proceeds to compare the next characters in both strings. - This process continues until one of the following conditions is met:
- Characters Differ: If
strcmp()
encounters characters at the same position that have different ASCII values, the comparison stops. - Null Terminator Encountered: If
strcmp()
reaches the null terminator () in both strings simultaneously, it means the strings are identical, and the function returns 0.
- Characters Differ: If
- If the characters are the same,
-
Determining the Return Value:
- Strings are Equal: If the loop completes because both strings reached the null terminator at the same time without finding any differing characters,
strcmp()
returns 0. str1
is Lexicographically Greater: Ifstrcmp()
finds a position where the character instr1
has a higher ASCII value than the character instr2
, it returns a positive value (specifically, the difference between the ASCII values of the first differing characters).
str1
is Lexicographically Smaller: Conversely, ifstrcmp()
finds a position where the character instr1
has a lower ASCII value than the character instr2
, it returns a negative value (specifically, the difference between the ASCII values of the first differing characters).
str1
is a Prefix ofstr2
(or vice versa): If one string is a prefix of the other (e.g., “Geek” and “Geeks”), the shorter string is considered lexicographically smaller. In this case,strcmp()
will return a negative value ifstr1
is the prefix and positive ifstr2
is the prefix.
- Strings are Equal: If the loop completes because both strings reached the null terminator at the same time without finding any differing characters,
Practical Examples of strcmp()
in C
Let’s explore various examples to illustrate the behavior of strcmp()
in different scenarios.
Example 1: Comparing 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: In this example, s1
and s2
are initialized with the same string “Hello”. strcmp(s1, s2)
returns 0 because the strings are identical, and the program correctly prints “Strings are equal”.
Example 2: Finding Lexicographically Greater String
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "zebra";
char str2[] = "apple";
int result = strcmp(str1, str2);
if (result == 0) {
printf("Strings are equaln");
} else if (result > 0) {
printf(""%s" is lexicographically greater than "%s"n", str1, str2);
} else {
printf(""%s" is lexicographically smaller than "%s"n", str1, str2);
}
return 0;
}
Output:
"zebra" is lexicographically greater than "apple"
Explanation: Here, “zebra” comes after “apple” in dictionary order. The comparison starts with ‘z’ and ‘a’. Since ‘z’ has a higher ASCII value than ‘a’, strcmp(str1, str2)
returns a positive value, indicating str1
is lexicographically greater.
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("Strings are equaln");
} else if (result > 0) {
printf(""%s" is lexicographically greater than "%s"n", string1, string2);
} else {
printf(""%s" is lexicographically smaller than "%s"n", string1, string2);
}
return 0;
}
Output:
"Cat" is lexicographically smaller than "Dog"
Explanation: “Cat” comes before “Dog” in dictionary order. The comparison begins with ‘C’ and ‘D’. ‘C’ has a lower ASCII value than ‘D’, so strcmp(string1, string2)
returns a negative value, indicating string1
is lexicographically smaller.
Example 4: Sorting an Array of Strings using strcmp()
and qsort()
strcmp()
is frequently used as a comparison function in sorting algorithms. The qsort()
function in C’s standard library is a general-purpose sorting function that can be used to sort arrays of any data type. We can leverage strcmp()
to sort an array of strings lexicographically.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Comparison function for qsort using strcmp
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]);
printf("Unsorted array: ");
for (int i = 0; i < n; i++) {
printf("%s ", names[i]);
}
printf("n");
// Sort the array of strings using qsort and strcmp
qsort(names, n, sizeof(names[0]), compareStrings);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%s ", names[i]);
}
printf("n");
return 0;
}
Output:
Unsorted array: Charlie Alice Bob David
Sorted array: Alice Bob Charlie David
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 dereferences them to get the actual string pointers. It then usesstrcmp()
to compare these strings.qsort()
function:qsort()
is called with the following arguments:names
: The array to be sorted.n
: The number of elements in the array.sizeof(names[0])
: The size of each element in the array (size of achar *
pointer).compareStrings
: The comparison function we defined.
qsort()
uses compareStrings
to determine the order of elements, effectively sorting the names
array lexicographically using strcmp()
as the underlying comparison mechanism.
C strcmp()
– Frequently Asked Questions
Q1: When does strcmp()
return zero?
strcmp()
returns zero when the two strings being compared are exactly identical. This means they have the same length and the same sequence of characters.
Q2: What does a positive return value from strcmp()
signify?
A positive return value from
strcmp()
indicates that the first string (str1
) is lexicographically greater than the second string (str2
). In dictionary terms,str1
would appear afterstr2
.
Q3: What does a negative return value from strcmp()
mean?
A negative return value from
strcmp()
indicates that the first string (str1
) is lexicographically smaller than the second string (str2
). In dictionary terms,str1
would appear beforestr2
.
Q4: Can strcmp()
be used to compare non-string data types in C?
No,
strcmp()
is specifically designed to compare C-style strings (null-terminated character arrays). It is not suitable for comparing other data types directly. For comparing numerical data or other structures, you would use different comparison operators or functions.
Q5: Is strcmp()
case-sensitive?
Yes,
strcmp()
is case-sensitive. It distinguishes between uppercase and lowercase letters based on their ASCII values. For example,"Apple"
and"apple"
are considered different strings bystrcmp()
. If you need case-insensitive string comparison, consider using functions likestrcasecmp()
(or_stricmp
on Windows), which are not part of the standard C library but are often available in POSIX-compliant systems and Windows, respectively.
Conclusion
The strcmp()
function is an essential tool for c string compare operations in C programming. Understanding its lexicographical comparison mechanism, return values, and practical applications, especially in sorting, is crucial for any C programmer. By mastering strcmp()
, you can effectively manipulate and compare string data in your C programs, building robust and efficient applications.