In this article we will discuss how to compare strings using strcmp().

strcmp() is a C Library function that helps to compare two strings i.e. char *

int strcmp ( const char * firstStr, const char * secondStr );

It iterates over both strings i.e. firstStr & secondStr in parallel and compares each character lexicographically until it finds NULL or ‘\0’ in any of the string.

Also, strcmp() compares string in case sensitive manner.

Its defined in following header file

#include<string.h>

What strcmp() returns :

  • 0   : If both strings are exactly equal.
  • <0 : If ASCII value of first mismatch character in firstStr is less than corresponding character in secondStr.
  • >0 : If ASCII value of first mismatch character in secondStr is less than corresponding character in firstStr.

Compare if two strings are equal using strcmp()

Suppose we have two strings i.e.

const char * str1 = "Hi This is sample";

const char * str2 = "Hi This is sample";

Now check if both strings are equal using strcmp()
// Check if both strings are equal
int result = strcmp(str1, str2);

// strcmp() returns 0 if both strings are exactly equal.
if (result == 0)
	std::cout << "Both Strings are equal" << std::endl;
else
	std::cout << "Both Strings are not equal";

Output:
Both Strings are equal

Here, strcmp() returned 0 because both the strings were exactly same.

Now, lets create a new string i.e.

const char * str3 = "Hi This is";

Now we have two different strings, Now check if both strings are not equal using strcmp() i.e.
// Check if both strings are equal
result = strcmp(str1, str3);

// strcmp() returns <0 or >0 if both strings are not equal.
if (result == 0)
	std::cout << "Both Strings are equal" << std::endl;
else
	std::cout << "Both Strings are not equal : strcmp returned : " << result << std::endl;

Output:
Both Strings are not equal : strcmp returned : 32

 

Here strcmp() returned 1 because ASCII value of first mismatch character in str2 is less than corresponding character in str1.

Complete example is as follows,

#include <iostream>
#include <string.h>

int main()
{

	const char * str1 = "Hi This is sample";

	const char * str2 = "Hi This is sample";

	// Check if both strings are equal
	int result = strcmp(str1, str2);

	// strcmp() returns 0 if both strings are exactly equal.
	if (result == 0)
		std::cout << "Both Strings are equal" << std::endl;
	else
		std::cout << "Both Strings are not equal";


	const char * str3 = "Hi This is";

	// Check if both strings are equal
	result = strcmp(str1, str3);

	// strcmp() returns <0 or >0 if both strings are not equal.
	if (result == 0)
		std::cout << "Both Strings are equal" << std::endl;
	else
		std::cout << "Both Strings are not equal : strcmp returned : " << result << std::endl;


	return 0;
}

Output:
Both Strings are equal
Both Strings are not equal : strcmp returned : 32

To Compile the above example in Linux Use following command,

g++ example.cpp

 

Join a list of 2000+ Programmers for latest Tips & Tutorials