#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12

    string letter detection?


    Is there a way that I can tell if a string has a certain letter in it? For example:

    string asdfa;
    cout<<"type in string";
    cin>>asdfa;


    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)
    {
    continue...
    }

    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!

    -andy
    hmmm...
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Use strchr() or strstr()
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    hmm not formiliar with those..ill do some research. Thanks!

    -andy
    hmmm...
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    andy3109,

    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:

    Code:
    string text;
    char looking_for = 'a';
    bool found_flag = false;
    
    for(int i=0; i<text.length(); i++)
    {
    	if(text[i]==looking_for)
    	{
    		found_flag=true;
    		break;
    	}
    }
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Location
    Southern California
    Posts
    73
    Rep Power
    15
    Use the STL:

    Code:
    #include <algorithm>
    
    std::string::size_type pos =0;
    
    pos = asdfa.find_first_of("x", 0);  // find "x"
    
    if (pos == std::string::npos)
        // nope
    else
        // found
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    hmm..both interesting ways..I understand 7stud more, but thanks for your reply vpopper!

    -andy
    hmmm...
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    what if you wanted to text a rage of letters, as A through F.
    hmmm...
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    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 08:52 PM.
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    got it to work..thanks man.
    hmmm...

IMN logo majestic logo threadwatch logo seochat tools logo