mercredi 27 juillet 2016

first repeated word in a sentence

I'm not sure where the bug is. I'm trying to find the first repeated word in a string where the delimiters are a space, tab, comma, colon, semicolon, dash, and period.

Does anyone see what I'm sure is an obvious error?

std::string repeat(std::string str) {
  std::set<std::string> seen;

  str.insert(str.end(), ' ');

  std::string tmp;
  for (auto const& s : str) {
    if (s != ' '&&
        s != '\t'&&
        s != '.'&&
        s != ','&&
        s !=':'&&
        s != ';'&&
        s != '-')
      tmp += s;
    else {
      if (seen.find(tmp) != seen.end())
        return tmp;
      else {
        seen.insert(tmp);
        tmp.clear();
      }
    }
  }

  return "no repeats";
}

Aucun commentaire:

Enregistrer un commentaire