0

Hey everoyne. I'm wondering If it's possible to construct a switch statement which takes in a character and determines weither that character is a number or not, or an alphabetical number.

switch(i) /*character*/ {
    case '0': /*ok it's a number, doesn't have to be zero, any number from 0-9*/
    case 'a': /*ok it's an alphabetical number, doesn't have to be a but any from a-z*/
} 

Or do I have to write for each case , like so

switch(i) {
    case '0': break;
    case '1': break;
    ....
    ....
    ....
    case '9': break;
    case 'a': break;
    ....
    ....
    .... 'z': break;
}

I feel like I've seen a lighter version of switch statment where they used tricks like this??

2
Contributors
2
Replies
4
Views
4 Years
Discussion Span
Last Post by enkitosh
0

You could do that, but there is a much easier way if you use the macros in ctype.h

if( isdigit(i) )
{
    // blabla
} else if( isalpha(i) )
{
   // blabla
 }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.