views:

112

answers:

6

Is there an easy way to check for digits 0-9 with a switch statement? I'm writing a program to check for certain characters as well as digits. Like checking for '\0', 'F' or 'f', and was wondering if there was also a way to check for 0-9 in a similar fashion. I know I can write a program to return true or false if a character is a digit 0-9, but wasn't sure how to use that with one of the cases in a switch statement. Like if I had:

const int lowerBound = 48;
const int upperBound = 57;

bool isDigit(char *digit)
{
    if (*digit >= lowerBound && *digit <= upperBound) {
        return true;
    }
    else {
        return false;
    }

Thanks! }

how I can go

switch (*singleChar) {
    case(???):
}
+2  A: 
switch(mychar) {

   case '0':
   case '1':
   case '2':
   ..
   // your code to handle them here
   break; 

   .. other cases
}

This is called 'fall-through' - if a case block does not terminate with a break, control flow continues at the next case statement.

Alexander Gessler
A: 

You could do this

char input = 'a';

switch (input)
{
  case 'a': // do something; break;
  // so on and so forth...
  default: break
}
Vino
+1  A: 

This would probably do it, but to be honest, it's pretty ugly. I'd probably use a different construct (maybe a regex?) unless I knew this was a major hotspot, and even then I'd profile.

switch (*singlChar) {
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
        // do stuff
        break;
    default:
        // do other stuff
}
Hank Gay
This is probably the most common/clear way of writing a case statement where multiple cases satisfy the condition. It would be interesting academically to see what assembly code is produced for this vs. others.
Buckwad
A: 

I think GCC supports a non-standard extension where you can do things like this:

switch(myChar)
{
case '0'...'2':
    // Your code to handle them here
    break;

.. other cases
}

But it is NON-STANDARD and will not compile under Visual Studio for example.

Cthutu
A: 

I would probably write the switch statement for the particular letters (´f´, ´F´...) and add the condition in the else block.

switch ( ch ) {
case 'f': // ...
   break;
case 'F': // ...
   break;
default:
   if ( isDigit(ch) ) {
   }
};

(Also note that there is a standard isdigit function from standard C in header <cctype> and another in <locale> that takes a locale as parameter and performs checks based on that locale)

David Rodríguez - dribeas
+1  A: 

Hang on! Why are you defining your own isDigit function when there's one already available in the function in the 'ctype.h' header file...Consider this:

char *s = "01234"; char *p = s;
while(*p){
    switch(*p){
        case 'A' :
            break;
        default:
            if (isdigit(*p)){
                puts("Digit");
                p++;
            }
            break;
    }
}

Hope this helps, Best regards, Tom.

tommieb75