February 25th, 2003, 04:23 PM
string letter detection?
Is there a way that I can tell if a string has a certain letter in it? For example:
cout<<"type in string";
if...(there is the letter 'a' in string)
cout << "Your string contained the letter 'a' which isn't allowed in my program";
else if (if there isn't letter 'a' in string)
So I guess I am askign if there is a way I can detect a letter in a string and react based on if the user has typed that letter or not. Thanks!
February 25th, 2003, 04:32 PM
February 25th, 2003, 04:55 PM
hmm not formiliar with those..ill do some research. Thanks!
February 25th, 2003, 05:48 PM
Those functions are going to implement a process whereby you examine each character in the string using array notation. Using the string length as the loop control, you can examine each character of the string like this:
char looking_for = 'a';
bool found_flag = false;
for(int i=0; i<text.length(); i++)
February 25th, 2003, 07:29 PM
Use the STL:
std::string::size_type pos =0;
pos = asdfa.find_first_of("x", 0); // find "x"
if (pos == std::string::npos)
February 25th, 2003, 08:29 PM
hmm..both interesting ways..I understand 7stud more, but thanks for your reply vpopper!
February 25th, 2003, 08:43 PM
what if you wanted to text a rage of letters, as A through F.
February 25th, 2003, 09:48 PM
All characters are stored as integer codes. You can look up the ASCII codes for all the characters--there's a table of codes in the appendix of my book, so check yours for the table.
ASCII codes for A-F are 65-70, so you can test whether text[i] is between 65 and 70.
Last edited by 7stud; February 25th, 2003 at 09:52 PM.
February 25th, 2003, 10:37 PM
got it to work..thanks man.