Understanding C++ strcmp() function

Filed Under: C++
C Strcmp() Function

Hello, folks! In this article, we will be focusing on the working of the C++ strcmp function in detail.

Getting started with C++ strcmp() function

C++ strcmp() function is an efficient way to compare two strings lexiographically.

The strcmp() function compares two input strings in a lexicographic manner and returns an integer value based on the outcome of the comparison of the two input strings.

By lexicographic comparison, we mean, that two strings are actually compared with their ASCII values of every character. If found same, zero is returned and the string is said to be equal.

Syntax:

strcmp(string1,string2)

Example:

#include<stdio.h> 
#include<string.h> 
  
int main() 
{  
      
    char str1[] = "Python"; 
    char str2[] = "Python"; 
      
  
    if(strcmp(str1, str2)==0)
    printf("Strings are equal");
    else
    printf("\nThe strings are not equal");
    
    return 0; 
} 

Output:

Strings are equal

Value returned by the strcmp() function

The strcmp() compares the strings lexicographically and returns either of the following values based on the comparison:

  • value = zero(0): The function returns 0, if both the strings are lexicographically equal i.e. both the strings are identical.
  • value > zero(0): It returns a value greater than zero, if the first non-matching character of the string1 has a greater ASCII value than the character present in the string2.
  • value < zero(0): It returns a value less than zero, if the first non-matching character of the string1 has less ASCII value than the character present in the string2.

Variant 1: The strcmp() function returns zero(0)

The strcmp() function returns zero if both the strings are identical.

Example:

#include<stdio.h> 
#include<string.h> 
  
int main() 
{  
      
    char str1[] = "Python"; 
    char str2[] = "Python"; 
      
   int val_strcmp=0;
    val_strcmp = strcmp(str1, str2);
    printf("The return value of strcmp() function: %d" , val_strcmp); 
    return 0; 
} 

Output:

The return value of strcmp() function: 0

Variant 2: The strcmp() function returns a value ‘greater than zero’

A value greater than zero is returned if the ASCII value of the first non-matching character of string1 is found to be greater than the ASCII value of the corresponding character of string2.

Lets’s take an example:

string1 = ‘yz’

string2 = ‘ab’

strcmp(string1,string2)

When two strings are not identical, the function returns the difference between the ASCII values of the first non-matching characters of the two strings.

In the above example, the function returns 24 because ASCII value of ‘y’ – ASCII value of ‘a’ is 24

i.e 121 – 97 = 24

Example:

#include<stdio.h> 
#include<string.h> 
  
int main() 
{  
      
    char str1[] = "wxyz"; 
    char str2[] = "abcd"; 
      
   int val_strcmp=0;
    val_strcmp = strcmp(str1, str2);
    printf("The return value of strcmp() function: %d" , val_strcmp); 
    return 0; 
} 

In the above snippet of code, the ASCII value of ‘w’ – ASCII value of ‘a’ is more than zero i.e. 119 – 97 = 22

Output:

The return value of strcmp() function: 22

Variant 3: The strcmp() function returns a value ‘less than zero’

The strcmp() function returns a value less than zero, if the ASCII value of the first non-matching character of the first string is less than the ASCII value of that particular character of the second string.

Consider the strings, string1 = ‘ab’ and string2 =’yz’

The ASCII value of ‘a’ is less than ASCII value of ‘y’ i.e. 97 – 121 = -24.

Thus, the value returned by the function is less than zero i.e. -24.

Example:

#include<stdio.h> 
#include<string.h> 
  
int main() 
{  
      
    char str1[] = "ab"; 
    char str2[] = "yz"; 
      
   int val_strcmp=0;
    val_strcmp = strcmp(str1, str2);
    printf("The return value of strcmp() function: %d" , val_strcmp); 
    return 0; 
} 

Output:

The return value of strcmp() function: -24

C++ strcmp() method at a glance!

  • The strcmp() function compares the characters of the strings in a lexicographic manner.
  • Moreover, the function returns an integer based on the comparison.
  • An integer value greater than zero is returned if the ASCII value of the first non-matching character of the first string is greater than the second one.
  • If the strings are identical, the function returns zero.
  • A value less than zero is returned by the function if the ASCII value of the first non-matching character of the first string is less than the second one

Conclusion

Thus, in this article, we have understood the working of strcmp() function in C++.


References

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages