views:

150

answers:

4

Hi,

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!

+7  A: 

You can use find_first_of

JRL
You can, but it's not really intended for this job. It's intended to find any one of a set of characters.
Jerry Coffin
The function is also overloaded to take only one character
Tomaka17
Not to mention that said set of characters could very well consist of a single character anyway. Using `string::find` would make your code slightly more concise, though, as the difference in functionality with `string::find_first_of` is irrelevant in this case.
JAB
@Jerry: not true and hardly worth a downvote, especially since there is the method signature `size_t find_first_of ( char c, size_t pos = 0 ) const;`
JRL
Yes, it is true. Yes, it's overloaded to take a single character, and yes, you can also pass a string that only contains one character (which is what the single-character version does, indirectly). Nonetheless, using "first_first_of" when you do *not* intend to find the first one of a set of characters is misleading at best, and a poor idea. When writing code, understanding by other people is primary, and by the compiler secondary (or *should* be anyway).
Jerry Coffin
@Amardeep: Given the question, the right (or at least best) answer is clearly `find`, not `find_first_of`. The subsequent discussion has shown only how the answer is poor rather than dead wrong -- hardly a good reason for an up-vote.
Jerry Coffin
@Jerry: We obviously have different criteria for voting. I only downvote if the answer is factually wrong or materially harms the knowledge base. I'll revoke it if the breach is corrected. I'll upvote if the answer offered something useful even when that useful thing may not be 100% germane to the original question. I feel that comments are a better mechanism to steer answers that are functionally correct but not optimal. I'm not preaching that you do the same but explaining why I did.
Amardeep
@Amardeep: apparently we do. I think anything further about it belongs on meta, not here though...
Jerry Coffin
+4  A: 

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

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

Amardeep
+9  A: 

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

Scharron
+1 because you offered documentation instead of pointing to the exact answer to this obvious homework question.
paercebal
@paercebal: How do you know it's not just a hobbyist learning to write a game program? Most homework assignments have tended to use char arrays and disallow library functions. Besides, you don't provide them the whole game just by telling them which function finds a character in a string. (Oh, +1 for the documentation).
Amardeep
@Amardeep : The way the question is formulated, the question itself, and some hints like "I'm using std::string (**obligatory**)". cpluplus.com is a very good C++ reference, and the author of the answer both pointed to it and gave a hint... ^_^ ...
paercebal
+1  A: 

Take a look at string::find

JLWarlow
Thx all, just really overlooked it :s