The following program could illustrates you about that how you could use the compare() function in the C++ language programming while using the compare() function in the programming as shown below :
Code:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string a ("green apple");
string b ("red apple");
if (a.compare(b) != 0)
cout << a << " is not " << b << "\n";
if (a.compare(6,5,"apple") == 0)
cout << "still, " << a << " is an apple\n";
if (b.compare(b.size()-5,5,"apple") == 0)
cout << "and " << b << " is also an apple\n";
if (a.compare(6,5,b,4,5) == 0)
cout << "therefore, both are apples\n";
return 0;
}
Output:
Code:
green apple is not red apple
still, green apple is an apple
and red apple is also an apple
therefore, both are apples
Bookmarks