Answers for "string compare c++"

C++
5

how to compare strings in c++

// comparing apples with apples
#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 applen";

  if (str2.compare(str2.size()-5,5,"apple") == 0)
    std::cout << "and " << str2 << " is also an applen";

  if (str1.compare(6,5,str2,4,5) == 0)
    std::cout << "therefore, both are applesn";

  return 0;
}
Posted by: Guest on July-14-2020
0

c++ compare strings

// comparing both using inbuilt function
int x = s1.compare(s2);

if (x !=0)
	//strings are not equal
else
	//strings are equal
Posted by: Guest on October-17-2021
0

c++ compare strings

if ((str1.compare(str2) == 0) || (str1 == str2))
  // they are EQUAL

// str1.compare(str2):
// < 0	Either the value of the first character that does not match is lower in the compared string, or all compared characters match but the compared string is shorter.
// > 0	Either the value of the first character that does not match is greater in the compared string, or all compared characters match but the compared string is longer.
Posted by: Guest on November-01-2021
0

cpp compare strings

//three methods to compare strings in c++
String strcmp() fucntion
In-built compare() function
c++ relational operators ('==','!=')
Posted by: Guest on October-25-2021
0

c++ compare

c++ compare values
Posted by: Guest on January-31-2021

Browse Popular Code Answers by Language