how to compare strings in c 2b 2b

Solutions on MaxInterview for how to compare strings in c 2b 2b by the best coders in the world

showing results for - "how to compare strings in c 2b 2b"
Brendon
02 Jul 2016
1// comparing apples with apples
2#include <iostream>
3#include <string>
4
5int main ()
6{
7  std::string str1 ("green apple");
8  std::string str2 ("red apple");
9
10  if (str1.compare(str2) != 0)
11    std::cout << str1 << " is not " << str2 << '\n';
12
13  if (str1.compare(6,5,"apple") == 0)
14    std::cout << "still, " << str1 << " is an apple\n";
15
16  if (str2.compare(str2.size()-5,5,"apple") == 0)
17    std::cout << "and " << str2 << " is also an apple\n";
18
19  if (str1.compare(6,5,str2,4,5) == 0)
20    std::cout << "therefore, both are apples\n";
21
22  return 0;
23}