In this example, you’ll learn How to compare two strings in C++ using if condition.
C++ Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <iostream> #include<stdlib.h> using namespace std; int main() { string s1,s2; //with case sensitive cout<<"String 1 : "; cin>>s1; cout<<"String 2 : "; cin>>s2; if(s1==s2) { cout<<"Equal"; } else { cout<<"Not Equal"; } } |
When you run the program, the output will be:
In the above program, we’ve two strings s1 and s2. We simply use equality operator (==
) to compare the two strings, which compares the value Java to C++ and prints Not Equal.