How to Use String compare() in C++?

Filed Under: C++
C String Compare() Function

Hey, folks! In this article, we will be focusing on the working of C++ string compare() function along with its variants.


Getting started with String compare() in C++

C++ String library contains a huge set of functions to work with the string data and manipulate the characters.

The string.compare() in C++ compares the string values in a lexicographical manner i.e. it compares the ASCII values of the characters of the comparable strings.

Syntax:

string1.compare(string2)

The compare() function returns 0, if the strings are exactly the same.

And, if the strings are not equal, the function returns either of the following values:

  • Value < 0: The function returns a value less than 0 if the ASCII value of the first non-matching character of string1 is less than ASCII value of the first character of string2.
  • Value > 0: The function returns a value greater than 0 if the ASCII value of the first non-matching character of string1 is greater than ASCII value of the first character of string2.

Example:

#include<iostream> 
using namespace std; 

int main() 
{ 
	string str1="Python"; 
	string str2="Python with Journaldev"; 
	if((str1.compare(str2))==0)
	    cout<<"Strings are equal!!";
	else
	    cout<<"Strings are not equal!!";
	return 0; 
} 

Output:

Strings are not equal!!

Variants of String compare() in C++

C++ String compare() function can be used in different forms to compare the strings accordingly.

Let us understand the different variants of the string.compare() function in the below section.


Variant 1: Comparing a portion of a string with the other using indexes

In this variation, we can compare a portion of string1 with string2 by using the below syntax:

Syntax:

string1.compare(start-index, end-length, string2)
  • start-index: The starting index of the string1 to be compared with string2.
  • end-length: The length of the string1 upto which the portion of string1 needs to be compared with string2.

Example:

#include<iostream> 
using namespace std; 

int main() 
{ 
	string str1="Python"; 
	string str2="Python with Journaldev"; 
	if((str2.compare(0, 6, str1))==0)
	    cout<<"Python with Journaldev contains Python";
	else
	    cout<<"Python with Journaldev does not contain Python";
	return 0; 
} 

In the above example, the portion of characters of str2 from index 0 to length 6 i.e. ‘Python’ is compared with str1.

Output:

Python with Journaldev contains Python

Variant 2: Comparing the string with the characters of the C-string

The string.compare() function can be used to compare the string with the characters of the C-string format using the below syntax:

Syntax:

string.compare(characters)

Example:

#include<iostream> 
using namespace std; 

int main() 
{ 
	string str1="Python"; 
	string str2="Python with Journaldev"; 
	if((str1.compare("Python")) == 0)
	    cout<<"Strings are equal!\n";
	else
	    cout<<"Strings are not equal!";
	
	if((str2.compare("Journaldev")) > 0)
	    cout<<"Python with Journaldev is greater than Journaldev";
	else
	    cout<<"Python with Journaldev is less than Journaldev";
	
	return 0; 
} 

Output:

Strings are equal!
Python with Journaldev is greater than Journaldev

Variant 3: Comparing portion of strings using string.compare() function

The string.compare() function can help us compare portions of the strings using the indexes.

Syntax:

string1.compare(start-index, end-length, string2, start-index, end-length)

Example:

#include<iostream> 
using namespace std;

int main() 
{ 
	string str1="Python"; 
	string str2="Python with Journaldev"; 
	if((str1.compare(0, 6, str2, 0, 6)==0))
	    cout<<"Strings are equal!\n";
	else
            cout<<"Strings are not equal!";
	
	return 0; 
} 

Output:

Strings are equal!

Variant 4: Comparing portion of string with C-string

We can compare some part of the string with the C-string by specifying the index using the below syntax:

Syntax:

string.compare(start-index, end-length,'C-string')

Example:

#include<iostream> 
using namespace std; 

int main() 
{ 
	string str1="Python"; 
	//string str2="Python with Journaldev"; 
	if((str1.compare(0, 6, "Python") == 0))
	    cout<<"Strings are equal!\n";
	else
	    cout<<"Strings are not equal!";
	
	return 0; 
} 

Output:

Strings are equal!

Variant 5: Comparing portion(characters) of string with a portion of C-string using compare() function

Syntax:

string.compare(start-index, end-length,'C-string',end-length)

Example:

#include<iostream> 
using namespace std; 

int main() 
{ 
	string str1="Python"; 
	//string str2="Python with Journaldev"; 
	if((str1.compare(0, 6, "Python",6) == 0))
	    cout<<"Strings are equal!\n";
	else
	    cout<<"Strings are not equal!";
	
	return 0; 
} 

Output:

Strings are equal!

Conclusion

Thus, we have come to the end of the explanation. In this article, we have understood the basic working of string compare() function along with the variations which can be made in the function.


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