Uses a switch statement to check letter input - C Statement

C examples for Statement:switch

Description

Uses a switch statement to check letter input

Demo Code

#include <stdio.h>
#include <ctype.h>
int main(void)
{
    char ch;/*from  w w w . j  ava2s.  c o m*/
    
    printf("Please type in a letter; type # to end my act.\n");
    while ((ch = getchar()) != '#')
    {
        if('\n' == ch)
            continue;
        if (islower(ch))     /* lowercase only          */
            switch (ch)
        {
            case 'a' :
                printf("Asia\n");
                break;
            case 'b' :
                printf("Beta\n");
                break;
            case 'c' :
                printf("Cat\n");
                break;
            case 'd' :
                printf("dusk\n");
                break;
            case 'e' :
                printf("egg\n");
                break;
            case 'f' :
                printf("fish\n");
                break;
            default :
                printf("default value!\n");
        }                
        else
            printf("only lowercase letters.\n");
        while (getchar() != '\n')
            continue;      /* skip rest of input line */
        printf("Please type another letter or a #.\n");
    }                      
    printf("from bo ok2s.com!\n");
    
    return 0;
}

Result


Related Tutorials