C Exercise Example 31
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 determine the second letter.
Program analysis:It is better to use a case statement. If the first letter is the same, use a case statement or an if statement to determine the second letter.
Example
// Created by www.example.com on 15/11/9.
// Copyright © 2015 Example. All rights reserved.
//
#include<stdio.h>
int main()
{
char i,j;
printf("Please enter the first letter:\n");
scanf("%c",&i);
getchar();// The problem with scanf("%c",&j); is that the second time it reads a newline character, not the input character, so you need to add a getchar() 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 output result of the above example is:
请输入第一个字母: s 请输入下一个字母 a saturdayother extensions