[C++} Using isalpha and isdigit to verify string

Hi All,

Sorry can anyone advice how can i use isalpha and isdigit to verify if the string is having alphanumeric?

I am struggling for 4 hrs on google search about using this two method.

I have a text file contain words like abc, 123 and a231. I want to show that the a231 is invalid.

I am able to read the text file now but i am stuck on the verifying on the content.

Thank you in advance.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
getline(fin, getAlphabets);
                     for (int j=0; getAlphabets[j]; j++)
                     {
                         getAlphabets[j] = tolower(getAlphabets[j]); // to lower case alphabets
                         }
if (isalpha(getAlphabets[i]))

                     {
                     cout << "Alphabet: " << getAlphabets << endl;
                     }
                     else {

                         if (isdigit(getAlphabets[i]))

                          cout << "Numbers: " << getAlphabets << endl;

                          }




Alphabet: tiv
Alphabet: v
Alphabet: go
Numbers: 4
Numbers: 13
Alphabet: ab1
Numbers: 1aa
Numbers: 578
Alphabet: xee
Alphabet: ssa




I have tried but i cannot differentiate that "ab1" for example is invalid. Can anyone advice? Thanks
what values does i iterate through?
i also not sure. i need to redo again i tink. my coding mess up o_O
If I am not mistaken what you want to do is check that ALL the characters of the word are either letters or numbers, right? You could declare 3 bool variables to know if the word has letters, numbers and non-alpharithmetic characters. Iterate through the length of the word and set each bool variable in the appropriate way (if isalpha(current_char) is true, set the has_alpha bool, if isdigit(current_char) is true, set the has_digit bool and if !isalnum(current_char) set the has_non_alnum bool). Of course depending of the values of these 3 bool variables you can decide that the word is not valid and stop iterating if necessary. Now, that's one solution that came to me while reading your post. Maybe you can find something better.
Last edited on
Hi Master Roshi,

Thank you very much for the advice. I will try digest what you have tell me. To tell you the truth, my C++ programming skill is around 2/10 if you grade me. I need time to digest them. And i tink i need to read my basics over and over again.

I will come back with post if I stuck with what you have posted. Thanks!
Hi,

Can any one assist to explain what does this mean?

for (counter = t.length(); counter >= 0; counter--)
I believe you will find this helpful: http://cplusplus.com/doc/tutorial/control/
Scroll down until you hit the for loop section.
In C++, just use one of the standard algorithms. A simple, one-pass, one-line method might be:
1
2
3
4
5
6
7
8
9
10
#include <algorithm>
#include <cctype>
#include <functional>
...

// Make sure my string 's' contains ONLY alphanumeric characters:
if (s.length() == std::count( s.begin(), s.end(), std::ptr_fun <int, int> ( std::isalnum ) ))
  cout << "yeah!\n";
else
  cout << "fooey!\n";

JSYK, the use of the std::ptr_fun<> functor is not optional in portable code. (So don't remove it even if your compiler doesn't complain.)

Hope this helps.
Last edited on
Thank you guys. I managed to solve my questions after sleepless night and crash course. Now I need to start with my task 2. woooo this is tough. I need to post a new thread regarding reading of text file. Haiz life is tough.
Topic archived. No new replies allowed.