Rewrite C Function: strcmp

Last Updated : 10 July, 2022 · 3 min read

articles banner image

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.

Structure of strcmp:

Definition: int strcmp(const char *s1, const char *s2);

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:

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>

int main( 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.

int our_strcmp ( const char * s1, const char * s2)
{
    
int i = 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.

# include <string.h>
# include <stdio.h>

int our_strcmp ( const char * s1, const char * s2)
{
    
int i = 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]);

						}
					
int main( void )
{
    
printf ( "1. our_strcmp: %d, strcmp: %d\n", our_strcmp ( "Equal" , "Equal" ), strcmp ( "Equal" , "Equal" ));
    
printf ( "2. our_strcmp: %d, strcmp: %d", our_strcmp ( "Not" , "Equal" ), strcmp ( "Not" , "Equal" ));
    
return ( 0 );
}

Possible output:

1. our_strcmp: 0, strcmp: 0
2. our_strcmp: 9, strcmp: 1

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:

2In which library is strcmp defined?

3Which statement about strcmp is false?

quiz completed!

Congrats!

Feel free to share your achievement below!