Example
C++
#include <iostream>
#include <string>
int main() {
std::string str1 = "Hello";
std::string str2 = "World";
std::string str3 = "Hello";
int result1 = str1.compare(str2);
int result2 = str2.compare(str1);
int result3 = str1.compare(str3);
std::cout << "Comparing 'Hello' & 'World': " << result1 << std::endl;
std::cout << "Comparing 'World' & 'Hello': " << result2 << std::endl;
std::cout << "Comparing 'Hello' & 'Hello': " << result3 << std::endl;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
Comparing 'Hello' & 'World': -1
Comparing 'World' & 'Hello': 1
Comparing 'Hello' & 'Hello': 0
In the example above, we compare three pairs of strings using the string compare() function. The first comparison between "Hello" & "World" returns a negative value (-1) because "Hello" comes before "World" in lexicographical order. The second comparison between "World" & "Hello" returns a positive value (1) because "World" comes after "Hello". Finally, the third comparison between "Hello" & "Hello" returns zero because the strings are equal.
Different Syntaxes for string::compare():
The string compare() function in C++ provides several syntaxes to compare strings in different ways. They are:
compare(const string& str)
-
This syntax compares the current string with another string str.
- It returns a negative value if the current string is less than str, zero if they are equal, & a positive value if the current string is greater than str.
Example
std::string str1 = "Hello";
std::string str2 = "World";
int result = str1.compare(str2);
compare(size_t pos, size_t len, const string& str)
-
This syntax compares a substring of the current string with another string str.
-
The substring starts at position pos & has a length of len characters.
- It returns a negative value if the substring is less than str, zero if they are equal, & a positive value if the substring is greater than str.
Example
std::string str1 = "Hello, World!";
std::string str2 = "World";
int result = str1.compare(7, 5, str2);
compare(const char* s)
-
This syntax compares the current string with a null-terminated C-style string s.
- It returns a negative value if the current string is less than s, zero if they are equal, & a positive value if the current string is greater than s.
Example
std::string str1 = "Hello";
const char* str2 = "World";
int result = str1.compare(str2);
compare(size_t pos, size_t len, const char* s)
-
This syntax compares a substring of the current string with a null-terminated C-style string s.
-
The substring starts at position pos & has a length of len characters.
- It returns a negative value if the substring is less than s, zero if they are equal, & a positive value if the substring is greater than s.
Example
std::string str1 = "Hello, World!";
const char* str2 = "World";
int result = str1.compare(7, 5, str2);
Frequently Asked Questions
What happens if the strings being compared have different lengths?
If the strings have different lengths, the comparison is performed until the end of the shorter string is reached. If all characters up to that point are equal, the longer string is considered greater.
Is string compare() case-sensitive?
Yes, string compare() is case-sensitive by default. It treats uppercase & lowercase characters as distinct. If you want to perform a case-insensitive comparison, you can convert both strings to lowercase or uppercase before comparing them.
Can I use string compare() to compare strings in reverse order?
Yes, you can use the string compare() function with reverse iterators to compare strings in reverse order. Alternatively, you can reverse the strings first & then perform the comparison using the standard syntaxes.
Conclusion
In this article, we have learned about the string compare() function in C++ & its various syntaxes. We explained how to compare entire strings, substrings, & C-style strings using different variations of the compare() function. We also discussed the return values of string compare() & their meanings. With the help of these concepts, you can effectively compare strings in your C++ programs & make decisions based on the comparison results. String comparison is a fundamental operation in programming, & mastering it will help you write more efficient & robust code.
You can refer to our guided paths on Code 360. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.