AppDividend
Latest Code Tutorials

C++ strcmp() Function Example | C++ Standard Library

0

C++ strcmp() is an inbuilt function that is used for string handling. It is defined in string.h header file. The strcmp() function is a predefined library which is used for comparing two strings of which null characters terminate the characters. The comparison is done lexicographically and returns an integer. The strcmp() function takes two strings as arguments and compares these two strings lexicographically.

C++ strcmp() Function

The strcmp() function takes two arguments: lhs_var and rhs_var. The function compares the contents of lhs_var and rhs_var lexicographically. The sign of the result is a sign of the difference between the first pairs of characters that differ in lhs_var and rhs_var.

TEMPLATE(PROTOTYPE)

See the following code.

int strcmp(const char* lhs_var, const char* rhs_var)

Here the function takes two parameters and returns an integer in the main function.

Parameters

The C++ strcmp() function takes 2 parameters lhs_var and rhs_var. It compares the characters of lhs_var and rhs_var lexicographically up to the strings get terminated by the null character.

The behavior of the function is undefined if the string is not ending with null characters.

Return Value

It returns an integer less than 0 if the first string is less than the second string, equal to zero if the first string is equal to the second string and greater than 0 if the first string is greater than the second string.

Difference between strncmp() and strcmp()

The main difference between strncmp() and strcmp() is that in case of strncmp() comparison is made up to x characters in the string while in the case of strcmp(), it is done till the null character terminates the strings.

Example programs on strcmp() function in C++

Example 1: Write a program to show the working of the strcmp() function.

See the following code example.

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
  char str_var1[20] = "Hello World!";
  char str_var2[20] = "Hello AI";
  int k;
  k = strcmp(str_var1, str_var2);
  if (k == 0)
  {
    cout << "Strings are equal.";
  }
  else
  {
    if (k > 0)
    {
      cout << "Strings are not equal and first string is greater.";
    }
    else
    {
      cout << "Strings are not equal and the second string is greater.";
    }
  }
}
Output
For output refer: Strings are not equal and the first string is greater.
Example 2: Write a program to compare two equal strings using strcmp() function.
#include <iostream>
#include <string.h>

using namespace std;

int main()
{
  char str_var1[20] = "Hello World!";
  char str_var2[20] = "Hello World!";
  int k;
  k = strcmp(str_var1, str_var2);
  if (k == 0)
  {
    cout << "String are equal.";
  }
  else
  {
    if (k > 0)
    {
      cout << "Strings are not equal and first string is greater.";
    }
    else
    {
      cout << "Strings are not equal and the second string is greater.";
    }
  }
}
Output
Strings are equal.

Conclusion

C++ strcmp() function returns zero if both strings are identical.

It returns the value greater than zero when the matching character of the left string has greater ASCII value than the character of a right string.

It returns a value less than zero when the matching character of the left string has lesser ASCII value than a character of the right string.

Finally, C++ strcmp() example is over.

See also

Functions in C++

Arrays in C++

String class in C++

C++ typedef

C++ Exception Handling

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.