Easy Tutorial
❮ C Examples Average Arrays C Exercise Example93 ❯

C Exercise Example 31

C Language Classic 100 Examples

Title: Please enter the first letter of the day of the week to determine which day it is. If the first letter is the same, continue to judge the second letter.

Program Analysis: Using a switch statement is better. If the first letter is the same, use a switch statement or an if statement to judge the second letter.

Example

//  Created by www.tutorialpro.org on 15/11/9.
//  Copyright © 2015 tutorialpro.org. All rights reserved.
//

#include<stdio.h>

int main()
{
    char i,j;
    printf("Please enter the first letter:\n");
    scanf("%c",&i);
    getchar();//The issue with scanf("%c",&j); is that the second input reads a newline character instead of the input character, so a getchar() is needed to consume the newline
    switch(i)
    {
        case 'm':
            printf("monday\n");
            break;
        case 'w':
            printf("wednesday\n");
            break;
        case 'f':
            printf("friday\n");
            break;
        case 't':
            printf("Please enter the next letter\n");
            scanf("%c",&j);
            if (j=='u') {printf("tuesday\n");break;}
            if (j=='h') {printf("thursday\n");break;}
        case 's':
            printf("Please enter the next letter\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:

Please enter the first letter:
s
Please enter the next letter
a
saturday

C Language Classic 100 Examples

❮ C Examples Average Arrays C Exercise Example93 ❯