compare string numbers c++ code example
Example 1: c++ compare char array
#include <cstring>
std::strcmp(<1st-char>,<2nd-char>)
#include <cstring>
if (std::strcmp(char_1,char_2) == 0) {
std::cout << "The char's that you compared match!" << std::endl;
}
else {
std::cout << "The char's that you compared DON'T match" << std::endl;
}
Example 2: how to compare strings in c++
#include <iostream>
#include <string>
int main ()
{
std::string str1 ("green apple");
std::string str2 ("red apple");
if (str1.compare(str2) != 0)
std::cout << str1 << " is not " << str2 << '\n';
if (str1.compare(6,5,"apple") == 0)
std::cout << "still, " << str1 << " is an apple\n";
if (str2.compare(str2.size()-5,5,"apple") == 0)
std::cout << "and " << str2 << " is also an apple\n";
if (str1.compare(6,5,str2,4,5) == 0)
std::cout << "therefore, both are apples\n";
return 0;
}