In this tutorial, we’ll learn methods to compare strings in C++. Consider a scenario wherein you are required to enter your name and password to login to a particular website. In such cases, at the back end, we need to build and script functions to check and compare the input string (login details) with the string stored in the data base.
So, relating it here, in this article, will be understanding the Ways to compare string in C++ language.
Techniques to Compare Strings in C++
Strings in C++ can be compared using either of the following techniques:
- String strcmp() function
- In-built compare() function
- C++ Relational Operators ( ‘==’ , ‘!=’ )
1. String strcmp() function in C++
C++ String has got in-built functions to manipulate and deal with data of string type. In order to compare two strings, we can use String’s strcmp()
function.
The strcmp()
function is a C library function used to compare two strings in a lexicographical manner.
Syntax:
1 |
int strcmp ( const char * str1, const char * str2 ); |
- The function returns 0 if both the strings are equal or the same.
- The input string has to be a char array of C-style string.
- The strcmp() compares the strings in a case-sensitive form as well.
Example 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include<iostream> using namespace std; #include<string.h> int main() { const char *str_inp1="JournalDEV"; const char *str_inp2="JournalDEv"; cout<<"String 1:"<<str_inp1<<endl; cout<<"String 2:"<<str_inp2<<endl; if (strcmp(str_inp1, str_inp2) == 0) cout << "nBoth the input strings are equal." << endl; else cout << "The input strings are not equal."; } |
Output:
1 2 3 4 |
<span style="color: #008000;"><strong>String 1:JournalDEV String 2:JournalDEv The input strings are not equal. </strong></span> |
Example 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include<iostream> using namespace std; #include<string.h> int main() { const char *str_inp1="Python"; const char *str_inp2="Python"; cout<<"String 1:"<<str_inp1<<endl; cout<<"String 2:"<<str_inp2<<endl; if (strcmp(str_inp1, str_inp2) == 0) cout << "nBoth the input strings are equal." << endl; else cout << "The input strings are not equal."; } |
Output:
1 2 3 4 |
<span style="color: #008000;"><strong>String 1:Python String 2:Python Both the input strings are equal. </strong></span> |
2. The compare() function in C++
C++ has in-built compare()
function in order to compare two strings efficiently.
The compare() function compares two strings and returns the following values according to the matching cases:
- Returns 0, if both the strings are the same.
- Returns <0, if the value of the character of the first string is smaller as compared to the second string input.
- Results out to be >0, when the second string is greater in comparison.
Syntax:
1 |
int compare (const string& string-name) const; |
Example 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include<iostream> using namespace std; int main() { string str_inp1("Python"); string str_inp2("Python"); cout<<"String 1:"<<str_inp1<<endl; cout<<"String 2:"<<str_inp2<<endl; int res = str_inp1.compare(str_inp2); if (res == 0) cout << "nBoth the input strings are equal." << endl; else if(res < 0) cout << "String 1 is smaller as compared to String 2n."; else cout<<"String 1 is greater as compared to String 2n."; } |
In the above example, we have compared string 1 with string 2. As clearly seen, both the strings are the same lexicographically, it returns 0.
Output:
1 2 3 4 |
<span style="color: #008000;"><strong>String 1:Python String 2:Python Both the input strings are equal. </strong></span> |
Example 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include<iostream> using namespace std; int main() { string str_inp1("Python"); string str_inp2("JournalDEV"); cout<<"String 1:"<<str_inp1<<endl; if (str_inp1.compare("Python") == 0) cout << "Strings are equal." << endl; else cout<<"Strings are not Equaln."; cout<<"nString 2:"<<str_inp2<<endl; if (str_inp2.compare("JournalDEv") == 0) cout << "Strings are equal." << endl; else cout<<"Strings are not Equal.n"; } |
In the above snippet of code, we have compared a string with another input string to the compare() function directly.
Output:
1 2 3 4 5 |
<span style="color: #008000;"><strong>String 1:Python Strings are equal. String 2:JournalDEV Strings are not Equal. </strong></span> |
3. Relational Operators in C++
C++ Relational operators such as ‘==’ and ‘!=’ can be useful in the comparison of string at an ease.
Syntax:
1 2 3 |
string1 == string 2 OR string1 != string2 |
Example 1: Using C++ ‘==’ operator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include<iostream> using namespace std; int main() { string str_inp1; string str_inp2; cout<<"Enter the String 1:n"; cin>>str_inp1; cout<<"Enter the String 2:n"; cin>>str_inp2; if (str_inp1 == str_inp2) cout <<"Strings are equal"<<endl; else cout <<"Strings are not equal"<< endl; } |
Output:
1 2 3 4 5 6 |
<span style="color: #008000;"><strong>Enter the String 1: Python Enter the String 2: PythoN Strings are not equal </strong></span> |
Example 2: Using C++ ‘!=’ operator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include<iostream> using namespace std; int main() { string str_inp1; string str_inp2; cout<<"Enter the String 1:n"; cin>>str_inp1; cout<<"Enter the String 2:n"; cin>>str_inp2; if (str_inp1 != str_inp2) cout <<"Strings are not equal"<<endl; else cout <<"Strings are equal"<< endl; } |
Output:
1 2 3 4 5 6 |
<span style="color: #008000;"><strong>Enter the String 1: Python Enter the String 2: Python Strings are equal </strong></span> |
Conclusion
In this article, we have understood various ways to compare strings in C++ Language.