std:🧵:compare() in C++ (original) (raw)
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 ****** 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:
Table of Content
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;
}
`
Output
Both 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.**