C++ Programs: Different Ways to Compare Strings
In C++, there are multiple ways to compare strings. We can use the equality (==) operator, the string class member functions, or the C-style string functions.
Syntax
Using the Equality Operator
if(string1 == string2) {
// code
}
Using the string Class Member Functions
if(string1.compare(string2) == 0) {
// code
}
Using the C-Style String Functions
if(strcmp(string1, string2) == 0) {
// code
}
Example
Using the Equality Operator
#include <iostream>
using namespace std;
int main() {
string string1 = "hello";
string string2 = "hello";
string string3 = "world";
if(string1 == string2) {
cout << "string1 and string2 are equal" << endl;
}
if(string1 == string3) {
cout << "string1 and string3 are equal" << endl;
}
return 0;
}
Using the string Class Member Functions
#include <iostream>
using namespace std;
int main() {
string string1 = "hello";
string string2 = "hello";
string string3 = "world";
if(string1.compare(string2) == 0) {
cout << "string1 and string2 are equal" << endl;
}
if(string1.compare(string3) == 0) {
cout << "string1 and string3 are equal" << endl;
}
return 0;
}
Using the C-Style String Functions
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char string1[] = "hello";
char string2[] = "hello";
char string3[] = "world";
if(strcmp(string1, string2) == 0) {
cout << "string1 and string2 are equal" << endl;
}
if(strcmp(string1, string3) == 0) {
cout << "string1 and string3 are equal" << endl;
}
return 0;
}
Output
Using the Equality Operator
string1 and string2 are equal
Using the string Class Member Functions
string1 and string2 are equal
Using the C-Style String Functions
string1 and string2 are equal
Explanation
In the above examples, we have defined three strings: string1, string2, and string3. We have used three different methods to compare these strings.
- In the first example, we have used the equality (==) operator to compare string1 and string2. Since they are equal, the output is "string1 and string2 are equal". We also compared string1 and string3, but they are not equal, so the output is nothing.
- In the second example, we have used the string class member function "compare" to compare string1 and string2. Since they are equal, the output is "string1 and string2 are equal". We also compared string1 and string3, but they are not equal, so the output is nothing.
- In the third example, we have used the C-style string function "strcmp" to compare string1 and string2. Since they are equal, the output is "string1 and string2 are equal". We also compared string1 and string3, but they are not equal, so the output is nothing.
Use
We can use any of the above methods to compare strings in C++. The best method to use depends on the specific situation and personal preference.
Important Points
- The equality (==) operator can only be used with string objects, not C-style strings.
- The string class member function "compare" returns 0 if the strings are equal, a negative value if the first string is less than the second string, and a positive value if the first string is greater than the second string.
- The C-style string function "strcmp" returns 0 if the strings are equal, a negative value if the first string is less than the second string, and a positive value if the first string is greater than the second string.
Summary
In summary, there are multiple ways to compare strings in C++. We can use the equality (==) operator, the string class member functions, or the C-style string functions. Each method has its advantages and disadvantages, and the best method to use depends on the specific situation and personal preference.