How can you check if the user inputs the same char more than once in a string?

So say you're having the user input a string like:

getline(cin,str);

How could you check for if the user put in the same letter twice?


For example, the user inputs "qwsq"

It should make the user try again until he makes an input that doesn't have any repeating characters.
http://www.cplusplus.com/reference/string/string/find/
1
2
3
4
5
6
7
bool has_duplicates (const string & str)
{   for (unsigned i=0; i<str.size()-1; i++)
    {   if (str.find(str[i], i+1) != string::npos)
          return true;  //  duplicate found
    }
    return false;   // No duplicates found
}
Last edited on
Thank you!
Topic archived. No new replies allowed.