-2

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")) {
      ~~~ ^
  • "*Doesn't work*" is **not** a good description of your problem. What doesnt work? Do you get an error? What kind? Where did you get the `equals` method from `std::string`? – Fureeish Sep 12 '20 at 11:28
  • this should not even compile. `std::string` has no `equals`. what you'll want to do is to count the number of X and the number of O in the string, and then compare the counts. – Cee McSharpface Sep 12 '20 at 11:28
  • Hey can you explain more? I am a newbi – Poorya Keshavarzi Sep 12 '20 at 11:29
  • Your problem is that there is no such string method called "equals". If you are a newbie, the best way to learn C++ would be [a good C++ textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Unfortunately, Stackoverflow is not a replacement for a textbook. – Sam Varshavchik Sep 12 '20 at 11:36
  • Whatever book, tutorial or class you're using or attending must have had enough information about how to solve this assignment before. Think about what have been told in the book/tutorial/class, go back and read old chapters or notes, try figure out if any of what have been taught so far could be used. – Some programmer dude Sep 12 '20 at 11:37
  • Also don't try to solve everything at once, use *divide and conquer*, where you divide each task or problem into smaller sub-tasks or sub-problems. Continue this division until it's not possible to split any more. Then do each of the smallest parts (sub-task or -problem) one by one, putting them together until you have a finished solution to the original task or problem. – Some programmer dude Sep 12 '20 at 11:38
  • Thanks guys you help a lot and thanks Sam TI hope that is helpful – Poorya Keshavarzi Sep 12 '20 at 11:46

1 Answers1

2

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.