The C++ == and != operators for string comparison program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the C++ operator!=  to test if the string object on the left side of the operator is not equal to the string object on the right side and operator== to test if the string object on the left side of the operator is equal to the string object on the right side in C++ programming

To show: How to use the C++ == and != operators to test if the string object on the left side of the operator is equal to the string object on the right side and to test if the string object on the left side of the operator is not equal to the string object on the right side in C++ programming respectively

 

// the C++ == and != operators code sample

#include <string>

#include <iostream>

using namespace std;

int main(void)

{

// declaring an objects of type basic_string<char>

string str1("testingone");

string str2("testingtwo");

cout<<"str1 string is = "<<str1<<endl;

cout<<"str2 string is = "<<str2<<endl;

 

// declaring a C-style string

char *str3 = "testingone";

cout<<"C-style str3 string is = char *str3 = \"testingone\" = "<<str3<<endl;

 

// comparison between left-side object of type basic_string and right-side object of type basic_string

cout<<"\nOperation: (str1 != str2)?"<<endl;

if(str1 != str2)

cout<<"str1 & str2 are not equal."<<endl;

else

cout<<"str1 & str2 are equal."<<endl;

 

// comparison between left-side object of C-style string type and right-side object of type basic_string

cout<<"\nOperation: (str3 != str2)?"<<endl;

if(str3 != str2)

cout<<"str3 & str2 are not equal."<<endl;

else

cout<<"str3 & str2 are equal."<<endl;

 

// comparison between left-side object of type basic_string and right-side object of C-style string type

cout<<"\nOperation: (str1 != str3)?"<<endl;

if(str1 != str3)

cout<<"str1 & str3 are not equal."<<endl;

else

cout<<"str1 & str3 are equal."<<endl;

 

// comparison between left-side object of type basic_string and right-side object of type basic_string

cout<<"\nOperation: (str1 == str2)?"<<endl;

if(str1 == str2)

cout<<"str1 & str2 are equal."<<endl;

else

cout<<"str1 & str2 are not equal."<<endl;

 

// comparison between left-hand object of C-style string type and right-hand object of type basic_string

cout<<"\nOperation: (str3 == str2)?"<<endl;

if(str3 == str2)

cout<<"str3 & str2 are equal."<<endl;

else

cout<<"str3 & str2 are not equal."<<endl;

 

// comparison between left-hand object of type basic_string and right-hand object of C-style string type

cout<<"\nOperation: (str1 == str3)?"<<endl;

if(str1 == str3)

cout<<"str1 & str3 are equal."<<endl;

else

cout<<"str1 & str3 are not equal."<<endl;

 

return 0;

}

 

Output example:

 

str1 string is = testingone

str2 string is = testingtwo

C-style str3 string is = char *str3 = "testingone" = testingone

 

Operation: (str1 != str2)?

str1 & str2 are not equal.

 

Operation: (str3 != str2)?

str3 & str2 are not equal.

 

Operation: (str1 != str3)?

str1 & str3 are equal.

 

Operation: (str1 == str2)?

str1 & str2 are not equal.

 

Operation: (str3 == str2)?

str3 & str2 are not equal.

 

Operation: (str1 == str3)?

str1 & str3 are equal.

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index