hi!
i'm trying to check if a string has numbers or chars, for example: char string[3];
how can i accomplish this using isdigit() or with other method?
hi!
i'm trying to check if a string has numbers or chars, for example: char string[3];
how can i accomplish this using isdigit() or with other method?
"Artificial Intelligence usually beats natural stupidity."
isdigit() and a loop.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Use a loop and check each character with isdigit().
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
isdigit() checks for digits and isalpha() checks for letters.
i tried this but it doesn't work properlyCode:char ipend[3]; size = strlen(ipend); for(i=0; i<=size; i++) { if( isdigit(ipend[i]) ) { MessageBox(hWnd, "digit", "digit", MB_OK); } else MessageBox(hWnd, "non digit", "non digit", MB_OK); }
it works fine with the 2 first characters of the string, but it doesn't seem to check the last one correctly.
"Artificial Intelligence usually beats natural stupidity."
By the last character, do you mean the \0 terminating character? Is there supposed to be something in ipend already?
That's because strlen() does not count the terminating null byte if its already populated. Use the sizeof operator instead.
> it works fine with the 2 first characters of the string, but it doesn't seem to check the last one correctly.
char [3] can only hold 2 characters, and a \0.
Otherwise, you're trashing memory.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
i tried: size = sizeof(ipend);
but the it doesn't work neither
"Artificial Intelligence usually beats natural stupidity."
Back to basics, how did you populate your string?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
With the code as it's shown, the string contains whatever garbage was in memory.
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
Code:char ipend[3]; size = sizeof(ipend); for(i=0; i<=size; i++) { if( isdigit(ipend[i]) ) { MessageBox(hWnd, "digit", "digit", MB_OK); } else MessageBox(hWnd, "non digit", "non digit", MB_OK); }
"Artificial Intelligence usually beats natural stupidity."
Wrong again. ipend[3] is out of bounds.
Stop "hacking" at it, learn and do it properly.
And ipend still contains garbage.
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010