// C++ program to check if
// two strings are identical
#include <bits/stdc++.h>
using namespace std;
int main()
{
char string1[100], string2[100];
// Get the strings which
// is to be checked
cin >> string1;
cout << "Enter the first string: " << string1;
// Get the strings which
// is to be checked
cin >> string2;
cout << "
Enter the second string: " << string2;
// Check if both strings are equal
cout << "
Are both strings same: ";
if (strcmp(string1, string2) == 0)
{
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
// This code is contributed by Akanksha Rai
#include <iostream>
using namespace std;
int main() {
string str1 = "mango";
string str2 = "apple";
int comparison = str1.compare(str2);
if (comparison == 0) {
cout << "Strings are equal." << endl;
} else if (comparison > 0) {
cout << "First string is greater than the second." << endl;
} else {
cout << "First string is less than the second." << endl;
}
}
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char key[] = "Softhunt.net";
char buffer[50];
do {
cout<<"Which is your favourite tutorial website? ";
cin>>buffer;
} while (strcmp (key,buffer) != 0);
cout<<"Answer is correct!!"<<endl;
return 0;
}
// CPP code for demonstrating
// string::compare (const string& str) const
#include<iostream>
using namespace std;
void compareOperation(string s1, string s2)
{
// returns a value < 0 (s1 is smaller than s2)
if((s1.compare(s2)) < 0)
cout << s1 << " is smaller than " << s2 << endl;
// returns 0(s1, is being compared to itself)
if((s1.compare(s1)) == 0)
cout << s1 << " is equal to " << s1 << endl;
else
cout << "Strings didn't match ";
}
// Driver Code
int main()
{
string s1("Geeks");
string s2("forGeeks");
compareOperation(s1, s2);
return 0;
}
//three methods to compare strings in c++
String strcmp() fucntion
In-built compare() function
c++ relational operators ('==','!=')
Code Example |
---|
Cpp :: c++ set swap |
Cpp :: visual studio getline not working |
Cpp :: c++ how to return an empty vector |
Cpp :: stl function to reverse an array |
Cpp :: template c++ |
Cpp :: polymorphism in c++ |
Cpp :: how to reverse a vector in c++ |
Cpp :: c++ read matttrix from text file |
Cpp :: exception handling class c++ |
Cpp :: looping in map c++ |
Cpp :: selection sort c++ |
Cpp :: prevent getting data from data-tooltip-content tippyjs |
Cpp :: c++ fill two dimensional array |
Cpp :: reference c++ |
Cpp :: heredar constructor c++ |
Cpp :: hierarchical inheritance in c++ employee |
Cpp :: c++ polymorphism |
Cpp :: even and odd in c++ |
Cpp :: minheap cpp stl |
Cpp :: Character cin(userInput) in c++ |
Cpp :: assign one vector to another c++ |
Cpp :: Start mongodb community server |
Cpp :: split string in c++ |
Cpp :: operator overloading in c++ |
Cpp :: memcpy in cpp |
Cpp :: sort an array in c++ |
Cpp :: deque |
Cpp :: set to vector c++ |
Cpp :: largest subarray with zero sum |
Cpp :: creating large maps cpp |