In this tutorial we will review, study examples of and rewrite strcmp function in C programming language.
For readers convenience, this tutorial is segregated into four parts.
Return: 0, if strings are equal. An int above 0, if s1 is bigger. An int below 0, if s2 is bigger.
Parameter list: (const char *s1) - first character array to be compared. (const char *s2) - second character array to be compared.
Header file:<string.h>.
1. So, what is the strcmp function in C?
The strcmp function compares two strings passed as arguments to this function in order to determine if
the strings contain identical values. Then, appropriate value is returned:
If the first string is bigger, strcmp returns value > 0.
If the first string is smaller, strcmp returns value < 0.
If both strings contain identical values, strcmp returns 0.
To use the strcmp function in C, string.h header needs to be included (#include
<string.h>).
Example 1: Usage of the strcmp function in C.
#include<string.h>
#include<stdio.h>
intmain(void)
{
printf("1. %d\n",strcmp("Equal", "Equal"));
printf("2. %d",strcmp("Not", "Equal"));
return(0);
}
Possible output:
1. 0
2. 1
2. How to rewrite the strcmp function in C?
In order to rewrite strcmp function accurately, we need to loop through two strings. Strings that are
received as parameters. Then, we have to compare each element inside both of the strings at the same
index. Just like it is done in the code example below:
Example 2: Implemented strcmp function in C.
intour_strcmp(const char* s1, const char* s2)
{
inti = 0;
unsigned char * s1_cpy = (unsigned char* ) s1;
unsigned char * s2_cpy = (unsigned char* ) s2;
while( s1_cpy[i] != '\0' && s2_cpy[i] != '\0')
{
if( s1_cpy[i]== s2_cpy[i] )
i++;
else
break;
}
return (s1_cpy[i] - s2_cpy[i]);
}
We are checking (line 8) if the characters at the same index are identical. So long the values
are identical, the conditional statement (line 8) returns true and the variable i is incremented. When this conditional statement returns false,
the loop will be stopped (line 11). The loop will also break, if both strings are identical,
because condition inside the while statement (line 6) returns false, in case one or both of the
strings reach the null terminating character.
Eventually, (line 13) the difference between characters will be returned. Now, if it so happens
that the loop was stopped because the conditional statement (line 8) returned false, the function
will return a non-zero value. Which is indicative that the strings do not hold identical values. If the
loop iterated till the end of both strings, however, then null terminating character will be subtracted
from a null terminating character, which results in 0 ('\0' - '\0' == 0 ). A zero is, in turn,
the return value of strcmp function in case both strings contain identical values.
Note: inside the return statement (on line x), we
are typecasting character to an unsigned char. This is done to
convert characters to their ASCII values for accurate results.
3. Testing the rewritten strcmp function
In order to test the rewritten strcmp function, we can combine code snippets presented above.
Example 3: Test of the rewritten strcmp function in C.
Now, attentive reader may have noticed that our_strcmp fuction returns 9, whereas the standard strcmp
function returns 1. This is, however, not an error. The strcmp function in C returns value > 0, when the first string is bigger than the second
one. Since 9 is greater than 0, the return condition of the strcmp function is satisfied. To understand,
why the first string is bigger than the second string in this case, you can explore this tutorial about ASCII in
C.
4. Quiz of the readers knowledge
Quiz : Rewrite C Function (strcmp)
1The
return value of strcmp("quizCoder", "quizCoder") is: