std::string::compare() in C++
Last Updated : 06 May, 2025
The string::compare() function in C++ is used to compare a string or the part of string with another string or substring. It is the member function of std::string class defined inside <string> header file. In this article, we will learn how to use string::compare() in C++.
The different ways to compare string using string::compare() function are as follows:
Compare Two Strings
The string::compare() method can be used to compare the one string with another string that is passed to it as argument.
Syntax
str1.compare(str2);
Parameters
- str1: First string with another string is to be compared.
- str2: Another string.
Return Value
- Returns 0 if str1 is equal to str2.
- Returns a positive integer when the first not-matching character in str1 has a greater ASCII value than the corresponding character in str2.
- Returns a negative integer when the first not-matching character in str1 has a lesser ASCII value than the corresponding character in str2.
Example
C++ // C++ program to show how to use string::compare() // to compare the two strings #include <bits/stdc++.h> using namespace std; int main() { string str1("Geeks"); string str2("Geeks"); // Comparing str1 and str2 if ((str1.compare(str2)) == 0) cout << "String Matched" << endl; else cout << "String Not Matched" << endl; return 0; }
Time Complexity: O(n), where n is the length of the smaller string.
Auxiliary space: O(1)
Compare Substring with Another String
The string::compare() method also supports the comparison between a part of the string with another string.
Syntax
str1.compare(pos, n, str2);
Parameters
- str1: First string with another string is to be compared.
- str2: Another string.
- pos: Starting index of substring taken from str1.
- n: Number of characters to compare.
Return Value
- Returns 0 if substring is equal to str2.
- Returns a positive integer when the first not-matching character in substring has a greater ASCII value than the corresponding character in str2.
- Returns a negative integer when the first not-matching character in substring has a lesser ASCII value than the corresponding character in str2.
Example
C++ // C++ program to show how to use string::compare() // to compare string with a part of another string #include <bits/stdc++.h> using namespace std; int main() { string str1("forGeeks"); string str2("Geeks"); // Compares 5 characters starting from index // 3 of str1 with str2 if ((str1.compare(3, 5, str2)) == 0) cout << "Substring Matched"; else cout << "Strings Not Matched"; return 0; }
Time Complexity: O(n), where n is the length of the substring.
Auxiliary space: O(1)
Explanation: Here we compare substring made by the 5 characters of str1 starting from index 3 i.e. “Geeks” with string str2 i.e. “Geeks”. As they are equal, ‘String Matched’ is printed.
Compare Substring with Another Substring
We can also compare the part of first string with the part of another string using the string::compare() method. In this case, we will need to define both the substring as shown below:
Syntax
str1.compare(pos1, n1, str2, pos2, n2);
Parameters
- str1: First string with another string is to be compared.
- pos1: Starting index of substring taken from str1.
- n1: Number of characters to compare from str1.
- str2: Another string.
- pos2: Starting index of substring taken from str2.
- n2: Number of characters to compare from str2.
Return Value
- Returns 0 if str1’s substring is equal to str2’s substring.
- Returns a positive integer Returns a positive integer when the first not-matching character in str1’s substring has a greater ASCII value than the corresponding character in str2’s substring.
- Returns a negative integer when the first not-matching character in str1’s substring has a lesser ASCII value than the corresponding character in str2. is greater than str2’s substring.
Example
C++ // C++ Program to show how to use string::compare() // to compare the one substring with another substring #include <bits/stdc++.h> using namespace std; int main() { string str1("Geeks"); string str2("forGeeks"); // Compares 5 characters of str1 starting from // index 0 with 5 characters of str2 starting // from index 3 if ((str1.compare(0, 5, str2, 3, 5)) == 0) cout << "Both Substring Matches"; else cout << "Substrings Not Matched"; return 0; }
OutputBoth Substring Matches
Time Complexity: O(n), where n is the length of the smaller substring.
Auxiliary space: O(1)
Explanation: Here we compare the first 5 character of str1 “Geeks” with 5 character from str2 “Geeks” starting with index 3.
Similar Reads
std::string::clear in C++
The string content is set to an empty string, erasing any previous content and thus leaving its size at 0 characters. Parameters: none Return Value: none void string ::clear () - Removes all characters (makes string empty) - Doesn't throw any error - Receives no parameters and returns nothing // CPP
1 min read
std::string class in C++
C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. The string class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character. String vs Character ArrayString Ch
8 min read
std::string::data() in C++
The data() function writes the characters of the string into an array. It returns a pointer to the array, obtained from conversion of string to the array. Its Return type is not a valid C-string as no '\0' character gets appended at the end of array. Syntax: const char* data() const; char* is the po
2 min read
std::basic_string::at in C++
Returns a reference to the character at the specified location pos. The function automatically checks whether pos is the valid position of a character in the string (i.e., whether pos is less than the string length), throwing an out_of_range exception if it is not. Syntax: reference at (size_type po
1 min read
std::string::assign() in C++
The member function assign() is used for the assignments, it assigns a new value to the string, replacing its current contents. Syntax 1: Assign the value of string str. string& string::assign (const string& str) str : is the string to be assigned. Returns : *this C/C++ Code // CPP code for
5 min read
std::string::push_back() in C++
The std::string::push_back() method in C++ is used to append a single character at the end of string. It is the member function of std::string class defined inside <string> header file. In this article, we will learn about std::string::push_back() method in C++. Example: [GFGTABS] C++ // C++ P
2 min read
std::string::append() in C++
The string::append() in C++ is used append the characters or the string at the end of the string. It is the member function of std::string class . The string::append() function can be used to do the following append operations: Table of Content Append a Whole StringAppend a Part of the StringAppend
3 min read
std::string::size() in C++
The std::string::size() function in C++ is a built-in method of the std::string class that is used to determine the number of characters in the string. All characters up to the null character are considered while calculating the size of the string. In this article, we will learn about string::size()
2 min read
std::string::back() in C++ with Examples
This function returns a direct reference to the last character of the string. This shall only be used on non-empty strings. This can be used to access the last character of the string as well as to append a character at the end of the string. Length of the string remains unchanged after appending a
2 min read
char* vs std:string vs char[] in C++
In this article, we are going to inspect three different ways of initializing strings in C++ and discuss differences between them. 1. Using char* Here, str is basically a pointer to the (const)string literal. Syntax: char* str = "This is GeeksForGeeks";Pros: 1. Only one pointer is required to refer
6 min read