Exes and Ohs (C++)

508 views Asked by At

This is what I am asked to do:Check to see if a string has the same amount of 'x's and 'o's. The method must return a boolean and be case insensitive. The string can contain any char.

Examples input/output:

XO("ooxx") => true
XO("xooxx") => false
XO("ooxXm") => true
XO("zpzpzpp") => true // when no 'x' and 'o' is present should return true
XO("zzoo") => false

And This is my code:

#include <iostream>
#include <string>
using namespace std;

bool XO(const std::string& str)
{
  if (str.equals("o", "x")) {
  return true;
  } else {
    return false;
  }
}
int Main() {
  XO("ooxx");
  XO("xooxx");
  XO("ooxXm");
  XO("zpzpzpp");
  XO("zzoo");
}

but it doesn`t work.What is my problem? This is the error I get By the way

main.cpp:12:11: error: no member named 'equals' in 'std::__cxx11::basic_string<char>'
  if (str.equals("o", "x")) {
      ~~~ ^
1

There are 1 answers

0
Christopher Yeleighton On

You need to count how many x characters are in the string. The result will be a number. Do the same with o characters. That will get you another number. Test whether the numbers are equal.