How can I make case-insensitive case insensitive?

How can I make the switch-case statement case insensitive? Say I did 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; } } 

He will respond only in capital letters. How to make him respond to lowercase letters?

+5
source share
4 answers

You just need to: -

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

etc. to continue the series.

In fact, what he does is that he goes around (dumps) to the bottom until he finds the first break statement that matches the case, thereby fulfilling all the cases that occur between them.

+14
source

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; } 
+30
source

Before switching () add:

 choice = toupper(choice); 

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

+6
source

You can give 2 cases one after another,

 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; } 
+1
source

Source: https://habr.com/ru/post/1204574/


All Articles