How Do I Compare Two Strings in C++?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • codegeekguy
    New Member
    • Sep 2021
    • 1

    How Do I Compare Two Strings in C++?

    Hi, I wanted to know if is there any simple code that can compare two different strings in C++ easily.
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 656

    #2
    How Do I Compare Two Strings in C++?
    - Relational operators
    - std::compare()
    - strcmp() {C library function}
    - Write custom code.

    Hi, I wanted to know if is there any simple code that can compare two different strings in C++ easily.
    Define 'simple code'.

    Comment

    • bulieme
      New Member
      • Oct 2021
      • 2

      #3
      example
      Code:
      str a = 'h'
      
      if( 'h' == a ){
      
      }else{
      
      }

      Comment

      • PaniniHead
        New Member
        • Oct 2021
        • 1

        #4
        Here, the comparison can be done in three ways.
        one - by using strcmp() function
        Two - by using compare() function
        or else we can directly compare two strings by using comparison operator.

        Here I am showing you one of the way by using comparison operators:
        let's say we have 2 strings

        string a=”abcd”;
        string b=”abcd”;

        So here we can simply use ‘==’ operator to check if they are equal or not
        if(a==b) return true;
        else return false;
        In order to know more about this you can read this article here.

        Comment

        • lucillemcmahan8
          New Member
          • Feb 2022
          • 1

          #5
          Strcmp

          strcmp might be the one code you could use for

          Comment

          • Shivam18
            New Member
            • Jun 2022
            • 2

            #6
            You can use strcmp function to compare two strings.

            I've written a code to compare two strings that they are equal or not.

            #include <iostream>
            using namespace std;

            int main ()
            {
            // declare string variables
            string str1;
            string str2;

            cout << " Enter the String 1: " << endl;
            cin >> str1;
            cout << " Enter the String 2: " << endl;
            cin >> str2;

            // use '==' equal to operator to check the equality of the string
            if ( str1 == str2)
            {
            cout << " String is equal." << endl;
            }
            else
            {
            cout << " String is not equal." << endl;
            }
            return 0;
            }

            Comment

            • BarryA
              New Member
              • Jun 2022
              • 19

              #7
              You can compare two C++ String using == operator

              Comment

              • Khushi16M
                New Member
                • Jul 2022
                • 1

                #8
                string s1= "xyz";
                string s2= "abc";
                if(s1!=s2){
                if(s1<s2){
                cout<<s2<<" is greater then "<<s1;
                }else{
                cout<<s1<<" is greater then "<<s2;
                }
                }else{
                court<<"Both the strings are equal.";
                }

                Comment

                Working...