I'm programming a program in C++ (typical game) in which you need to guess a letter and it will check if it is present in a string.
For example
Secret String: I like to program.
Guess1: 'a'
Display: . .... .. .....a...
Etc.
But i don't know how to see if a character is in this secret string.
I'm using std::string (obligatory)
Any help is appreciated!
contains() method searches the sequence of characters in the given string. It returns true if sequence of char values are found in this string otherwise returns false. Here conversion of CharSequence to a String takes place and then indexOf method is called.
Python3. Method 2 : Using str. find() str. find() method is generally used to get the lowest index at which the string occurs, but also returns -1, if string is not present, hence if any value returns >= 0, string is present, else not present.
Begin by learning searching in a documentation like : http://www.cplusplus.com/reference/string/string/ . (Hint : you want to "find" something ... )
You can use find_first_of
There are several methods in std::string that would help:
find() rfind() find_first_of() find_last_of() substr()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With