Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to see if a character is present in a string

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!

like image 480
Cissmayazz Avatar asked Jul 21 '10 15:07

Cissmayazz


People also ask

How do you check if a character is present in a string?

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.

How do you check a character is present in a string in Python?

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.


3 Answers

Begin by learning searching in a documentation like : http://www.cplusplus.com/reference/string/string/ . (Hint : you want to "find" something ... )

like image 92
Scharron Avatar answered Sep 21 '22 06:09

Scharron


You can use find_first_of

like image 42
JRL Avatar answered Sep 20 '22 06:09

JRL


There are several methods in std::string that would help:

find() rfind() find_first_of() find_last_of() substr()

like image 40
Amardeep AC9MF Avatar answered Sep 19 '22 06:09

Amardeep AC9MF