开发者

Checking for numbers in a switch statement

开发者 https://www.devze.com 2022-12-20 16:14 出处:网络
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 wonderi

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;
    }
}

how I ca开发者_运维百科n go

switch (*singleChar) {
    case(???):
}


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.


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;
    }
}


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)


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
}


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.


You could do this

char input = 'a';

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


Please use below code.

switch (*singleChar) {

         case 48 ... 57: return true;

         default: return false; 

}


How about:

int isdigit(char input) {
    return (input < '0' || input > '9') ? 0 : 1; 
}

...

if(isdigit(currentChar)) {
    ...
}
else {
    switch(currentChar) {
        case 'a' {
            ...
            break;
        }
        ...
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消