471,596 Members | 1,406 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,596 software developers and data experts.

How Do I Compare Two Strings in C++?

codegeekguy
1 2Bits
Hi, I wanted to know if is there any simple code that can compare two different strings in C++ easily.
Sep 26 '21 #1

✓ answered by bulieme

example
Expand|Select|Wrap|Line Numbers
  1. str a = 'h'
  2.  
  3. if( 'h' == a ){
  4.  
  5. }else{
  6.  
  7. }
  8.  

7 27248
dev7060
611 Expert 512MB
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'.
Oct 1 '21 #2
bulieme
2 2Bits
example
Expand|Select|Wrap|Line Numbers
  1. str a = 'h'
  2.  
  3. if( 'h' == a ){
  4.  
  5. }else{
  6.  
  7. }
  8.  
Oct 6 '21 #3
PaniniHead
1 Bit
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.
Oct 21 '21 #4
strcmp might be the one code you could use for
Feb 19 '22 #5
Shivam18
2 2Bits
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;
}
Jun 24 '22 #6
BarryA
8 Nibble
You can compare two C++ String using == operator
Jul 4 '22 #7
Khushi16M
1 Bit
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.";
}
Jul 13 '22 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by J.W. | last post: by
1 post views Thread by al | last post: by
3 posts views Thread by Drew | last post: by
14 posts views Thread by Samuel R. Neff | last post: by
3 posts views Thread by Jim Carlock | last post: by
reply views Thread by Diego Martins | last post: by
6 posts views Thread by shapper | last post: by
2 posts views Thread by Thenu | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.