Strings
Working with strings in C. String manipulation functions from the `string.h` library.
String Comparison in C
Explanation: String Comparison and the strcmp
Function
In C, strings are represented as arrays of characters, terminated by a null character ('\0'). Unlike some other languages, you cannot directly compare strings using operators like ==
. Using ==
would compare the *addresses* of the string arrays in memory, not the contents of the strings themselves.
The strcmp
function is a standard library function (found in string.h
) that is used to compare two strings lexicographically. It compares the strings character by character until it finds a difference or reaches the end of one or both strings.
The general syntax for using strcmp
is:
#include <string.h>
int strcmp(const char *str1, const char *str2);
Where:
str1
: A pointer to the first string to be compared.str2
: A pointer to the second string to be compared.
Comparing Strings Lexicographically with strcmp
Lexicographical comparison means comparing strings based on the ASCII values of their characters. The strcmp
function returns an integer value based on the comparison.
Return Values of strcmp
The strcmp
function returns:
0
: If the strings are identical.- A negative value (e.g., -1): If
str1
comes beforestr2
lexicographically (i.e.,str1
is "less than"str2
). - A positive value (e.g., 1): If
str1
comes afterstr2
lexicographically (i.e.,str1
is "greater than"str2
).
Important: The *exact* negative or positive value returned is implementation-defined and should not be relied upon. Only check if the value is negative, positive, or zero.
Example Code
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "apple";
char str2[] = "banana";
char str3[] = "apple";
char str4[] = "ant";
int result1 = strcmp(str1, str2); // "apple" vs. "banana"
int result2 = strcmp(str1, str3); // "apple" vs. "apple"
int result3 = strcmp(str1, str4); // "apple" vs. "ant"
int result4 = strcmp(str4, str1); // "ant" vs. "apple"
printf("strcmp(\"%s\", \"%s\") = %d\n", str1, str2, result1);
printf("strcmp(\"%s\", \"%s\") = %d\n", str1, str3, result2);
printf("strcmp(\"%s\", \"%s\") = %d\n", str1, str4, result3);
printf("strcmp(\"%s\", \"%s\") = %d\n", str4, str1, result4);
if (strcmp(str1, str2) < 0) {
printf("\"%s\" comes before \"%s\"\n", str1, str2);
} else if (strcmp(str1, str2) > 0) {
printf("\"%s\" comes after \"%s\"\n", str1, str2);
} else {
printf("\"%s\" is equal to \"%s\"\n", str1, str2);
}
return 0;
}
Explanation of the example:
strcmp(str1, str2)
compares "apple" and "banana". Since 'a' comes before 'b' in the ASCII table, `str1` is less than `str2`, so a negative value is returned.strcmp(str1, str3)
compares "apple" and "apple". They are identical, so 0 is returned.strcmp(str1, str4)
compares "apple" and "ant". Since 'p' comes after 'n' in "apple" compared to "ant", `str1` is greater than `str4`, returning a positive value.strcmp(str4, str1)
compares "ant" and "apple". Since 'n' comes before 'p' in "ant" compared to "apple", `str4` is less than `str1`, returning a negative value.