Comparing two strings in C++

C++ | Comparing two strings program: Here, we are going to see how to compare two strings in C++? We have discussed two methods here and also discussed which one is better to use and which not.
Submitted by Radib Kar, on July 23, 2020

Prerequisite: std::string::compare()

Strings in C++

String in C++ is considered as inbuilt data type like int, char, etc. That's why there have been various methods to compare strings. Below are the method description and examples.

How strings are compared?

Given two strings, how you will compare which one is greater and which one is lesser or whether they are equal or not. It's the same way as we find in a word dictionary. Like the string having a smaller character at first will be smaller.

For example, "abc" is smaller than "d" whereas "includehelp" is smaller than "india". In the first case, the first uncommon character appears at index 0 which are 'a' and 'd' respectively. Thus "abc" will be lesser than "d". In the second example, after the common character set 'i', 'n', the first uncommon characters are 'c' and 'd' and 'c' is smaller. So "includehelp" is smaller than "india". Another example can be "abc" and "abcd" where "abc" is of course smaller as it's itself the prefix of "abcd".

1) Comparing two string (Using relational operator)

As we mentioned since the string is considered to be a built-in data type, we can use relational operators to compare strings just like comparing other built-in data types like int, char, etc.

The relational operators are '<', '>', '==', '!=' which have been used in the following.

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string s1, s2;
 
    cout << "Enter string1:\n";
    cin >> s1;
    cout << "Enter string2:\n";
    cin >> s2;

    if (s1 < s2) {
        cout << s1 << " is smaller, " << s2 << " is greater\n";
    }
    else if (s1 > s2) {
        cout << s2 << " is smaller " << s1 << " is greater\n";
    }
    else {
        cout << "Both strings are equal\n";
    }

    return 0;
}

Output:

RUN 1:
Enter string1:
abc
Enter string2:
d
abc is smaller, d is greater


RUN 2:
Enter string1:
includehelp
Enter string2:
india
includehelp is smaller, india is greater


RUN 3:
Enter string1:
abcd
Enter string2:
abc
abc is smaller abcd is greater

2) Comparing two string (Using std::compare)

In C++, we have STL function compare() which compares to string and below is the syntax:

int compare (const string& str) const;

Below are the results based on the comparisons:

  1. Function returns 0 if both compared and comparing string is equal.  
  2. Function returns negative(<0) if compared string < comparing string.
  3. Function returns positive(>0) if compared string > comparing string.
#include <bits/stdc++.h>
using namespace std;

int main()
{
    string s1, s2;
 
    cout << "Enter string1:\n";
    cin >> s1;
    cout << "Enter string2:\n";
 
    cin >> s2;
 
    //s1 comparing string which invokes the function
    //s2 is compared string which is passed in argument
 
    int k = s1.compare(s2);
    
    if (k < 0) {
        cout << s1 << " is smaller, " << s2 << " is greater\n";
    }
    else if (k > 0) {
        cout << s2 << " is smaller " << s1 << " is greater\n";
    }
    else { //k==0
        cout << "Both strings are equal\n";
    }

    return 0;
}

Output:

Enter string1:
abcaaa
Enter string2:
abcba
abcaaa is smaller, abcba is greater

Which method is better?

If we are comparing the whole strings then it's, of course, better to use the relative operator. But the advantage of compare() function is that we can do a lot of stuff with this. Just for example, we can compare substrings too.





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.