Latest web development tutorials

C Exercise Example 31

100 cases of classic C language 100 cases of classic C language

Title: Please enter the first letter of the week to determine what day of the week, if the first letter of the same, then continue to determine the second letter.

Program Analysis: The case statement is better, if the first letter, as it is determined by the case statement or if statement to determine the second letter.

Source Code:

//  Created by www.w3big.com on 15/11/9.
//  Copyright © 2015年 本教程. All rights reserved.
//

#include<stdio.h>

int main()
{
    char i,j;
    printf("请输入第一个字母:\n");
    scanf("%c",&i);
    getchar();//scanf("%c",&j);的问题,第二次是读入的一个换行符,而不是输入的字符,因此需要加一个getchar() 吃掉换行符
    switch(i)
    {
        case 'm':
            printf("monday\n");
            break;
        case 'w':
            printf("wednesday\n");
            break;
        case 'f':
            printf("friday\n");
            break;
        case 't':
            printf("请输入下一个字母\n");
            scanf("%c",&j);
            if (j=='u') {printf("tuesday\n");break;}
            if (j=='h') {printf("thursday\n");break;}
        case 's':
            printf("请输入下一个字母\n");
            scanf("%c",&j);
            if (j=='a') {printf("saturday\n");break;}
            if (j=='u') {printf("sunday\n"); break;}
        default :
            printf("error\n"); break;
    }
    return 0;
}

The above example output is:

请输入第一个字母:
s
请输入下一个字母
a
saturday

100 cases of classic C language 100 cases of classic C language