Create a C++ program to compare the length of two strings.
Answers
Answered by
2
#include <iostream>#include <string>using namespace std;
int main()
{
string s1, s2;
cout<< "string1: "; cin>>s1;
cout<< "string2: " ; cin>>s2;
if ( s1.size()==s2.size() )
cout<< "Strings:" + s1 + ":" + s2 + "are of same length";
else
cout << "Strings:" + s1 + ":" + s2 + " are NOT of same length" ;
// if you want to compare the two strings then:
if (s1.compare(s2))
cout<< "Strings:" + s1 + ":" + s2 + "are NOT same";
else
cout << "Strings:" + s1 + ":" + s2 + "are Same" ;
return 0;
}
int main()
{
string s1, s2;
cout<< "string1: "; cin>>s1;
cout<< "string2: " ; cin>>s2;
if ( s1.size()==s2.size() )
cout<< "Strings:" + s1 + ":" + s2 + "are of same length";
else
cout << "Strings:" + s1 + ":" + s2 + " are NOT of same length" ;
// if you want to compare the two strings then:
if (s1.compare(s2))
cout<< "Strings:" + s1 + ":" + s2 + "are NOT same";
else
cout << "Strings:" + s1 + ":" + s2 + "are Same" ;
return 0;
}
Answered by
1
#include <iostream>#include <string>using namespace std;
int main()
{
string s1, s2;
cout<< "string1: "; cin>>s1;
cout<< "string2: " ; cin>>s2;
if ( s1.size()==s2.size() )
cout<< "Strings:" + s1 + ":" + s2 + "are of same length";
else
cout << "Strings:" + s1 + ":" + s2 + " are NOT of same length" ;
// if you want to compare the two strings then:
if (s1.compare(s2))
cout<< "Strings:" + s1 + ":" + s2 + "are NOT same";
else
cout << "Strings:" + s1 + ":" + s2 + "are Same" ;
return 0;
}
int main()
{
string s1, s2;
cout<< "string1: "; cin>>s1;
cout<< "string2: " ; cin>>s2;
if ( s1.size()==s2.size() )
cout<< "Strings:" + s1 + ":" + s2 + "are of same length";
else
cout << "Strings:" + s1 + ":" + s2 + " are NOT of same length" ;
// if you want to compare the two strings then:
if (s1.compare(s2))
cout<< "Strings:" + s1 + ":" + s2 + "are NOT same";
else
cout << "Strings:" + s1 + ":" + s2 + "are Same" ;
return 0;
}
Similar questions