473,462 Members | 1,399 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 27676
dev7060
638 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
19 16bit
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

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

Similar topics

2
by: J.W. | last post by:
How do I compare strings in javascript? The "==" double equals or "!=" doesn't seem to work in this case. I'm sure string comparison has been explained before but searching Google didn't find...
1
by: al | last post by:
To compare two strings, not just to see if they are "equal" ("abcd"="abcd") but tell their alphabetical order: string a, b; a = "abcd"; b = "bbcd"; Can all the ways below to do such...
3
by: Drew | last post by:
Hello - I am a converted VB programmer. What I am trying to do it compare two strings in an if statement. The problem is that when I use string.compare it always returns a negative 1. I have...
14
by: Samuel R. Neff | last post by:
Why would you cast two strings to objects to compare them? I saw code in an MS sample on MSDN and don't get it. if ( (object)name == (object)attr.name ) { both "name" and "attr.name" are...
3
by: Jim Carlock | last post by:
I'm creating a lot of arrays. Some of the arrays carry words that get duplicated... For example: $aCities = {"Durham", "Raleigh", "Raleigh-Durham", "Salem", "Winston", "Winston-Salem" } I've...
0
by: Diego Martins | last post by:
Hi! The following code snippet: namespace { bool charCompare(char a, char b) { return tolower(a) < tolower(b); } } bool compareString(const std::string & s1, const std::string & s2) {
1
by: lawrence k | last post by:
Can I check alphabeticallness as easily as "aaa" "bgeoid" ? $var1 = "cccccc"; $var2 = "f"; if ($var1 $var2) {
2
by: jonathan184 | last post by:
Hi I am trying to use if statements to search strings on each line and if it finds matches to the string write to a variable and then echo variable and the loop starts again. The match is being...
6
by: shapper | last post by:
Hello, I want to compare two strings in a Linq Query. In this case "Car", "cAr", "CAR" would all be the same. Should I use ==, equals, ... ? What is the best way to do this?
2
by: Thenu | last post by:
Im doing my project in Asp .net with Vb .net Coding.... I have some Problem in doing my project... If anybody Guide me means it would be great pleasure for me.. I want to compare the strings.Its...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.