This is the code:
char s[101], s1[101];
cin >> s >> s1;
cout << stricmp(s, s1);
I tried to declare s
and s1
as std::string
, but it didnt work. Can someone explain please why stricmp()
works with char[]
but not std::string
?
This is the code:
char s[101], s1[101];
cin >> s >> s1;
cout << stricmp(s, s1);
I tried to declare s
and s1
as std::string
, but it didnt work. Can someone explain please why stricmp()
works with char[]
but not std::string
?
That is because stricmp()
doesn' t take std::string
values as arguments.
Use std::basic_string::compare()
instead.
std::string s ("s");
std::string s1 ("s1");
if (s.compare(s1) != 0) // or just if (s != s1)
std::cout << s << " is not " << s1 << std::endl;
If you need a case-insensitive comparison, you need to make your own function, maybe using std::tolower()
as in this example, or just boost::iequals()
as in this other example.
You may want to consider converting the strings to all upper or all lower case before comparing:
std::string s1;
std::string s2;
std::cin >> s1 >> s2;
std::transform(s1.begin(), s1.end(),
s1.begin(),
std::tolower);
std::transform(s2.begin(), s2.end(),
s2.begin(),
std::tolower);
if (s1 == s2)
{
std::cout << "s1 and s2 are case insensitive equal.\n";
}
else
{
std::cout << "s1 and s2 are different.\n";
}