5

How can I make a switch-case statement to not be case sensitive? Say I made something like this:

#include <stdio.h>

char choice;
int main ()
{
char choice;
printf("Will you choose A,B, or C?\n>");
scanf(" %c", &choice);

switch(choice)
{
    case 'A': 
         printf("The First Letter of the Alphabet");
         break;
    case 'B':
         printf("The Second Letter of the Alphabet");
         break;
    case 'C':
         printf("The Third Letter of the Alphabet");
         break;
}
}

It would only respond to capital letters. How do I make it respond to lower case letters?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Jeremy
  • 181
  • 1
  • 2
  • 10

4 Answers4

32

toupper in <ctype.h> converts a character to uppercase:

#include <stdio.h>
#include <ctype.h>

char choice;
int main ()
{
printf("Will you choose A,B, or C?\n>");
scanf(" %c", &choice);

switch(toupper(choice))  // Changed line
{
    case 'A': 
         printf("The First Letter of the Alphabet");
         break;
    case 'B':
         printf("The Second Letter of the Alphabet");
         break;
    case 'C':
         printf("The Third Letter of the Alphabet");
         break;
}
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
  • 1
    I actually prefer this method. Seems more elegant and less error prone. Especially in this case where you are looking at case-insensitivising specifically. – JoErNanO Oct 13 '14 at 17:12
  • 2
    @JoErNanO, in the general case yes, but it's also good to understand that the case construct can take several `case` after each other. I like both answers. If you user `toupper()`, you need to be aware of the limitations and quirks of runtime, if applicable. http://stackoverflow.com/questions/12493496/c-c-utf-8-upper-lower-case-conversions If you use several `case`, you need to be aware of the limitations of the compiler. It might only accept ASCII, for instance. Trade-offs. :) – Prof. Falken Oct 13 '14 at 17:40
  • @Prof. Falken I absolutely agree. – JoErNanO Oct 15 '14 at 22:24
14

You simply need this :-

switch(choice)
{
case 'A': 
case 'a':
     printf("The First Letter of the Alphabet");
     break;
case 'B':
case 'b':
     printf("The Second Letter of the Alphabet");
     break;
case 'C':
case 'c':
     printf("The Third Letter of the Alphabet");
     break;
}

and so on to continue your series.

Actually,what it does is that it bypasses(skims) upto bottom until it finds the first break statement matching the case thereby executing all the cases encountered in between!!!

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
6

Before the switch(), add:

choice = toupper(choice);

And if you haven't already got it, #include <ctype.h> to get the prototype.

JohnH
  • 2,713
  • 12
  • 21
1

You can give 2 cases one by one,

switch(choice)
{
case 'A':
case 'a':
     printf("The First Letter of the Alphabet");
     break;
case 'B':
case 'b':
     printf("The Second Letter of the Alphabet");
     break;
case 'C':
case 'c':
     printf("The Third Letter of the Alphabet");
     break;
}
Haris
  • 12,120
  • 6
  • 43
  • 70