Write a console program: Code the program so that it displays the color of the i
ID: 3842448 • Letter: W
Question
Write a console program:
Code the program so that it displays the color of the item whose item number is entered by the user. All the item numbers contain exactly five characters. Allitems are available in four colors: blue, green, red, white. The third character in the item number indicates the item's color, as follows:
B or b = Blue
G or g = Green
R or r = Red
W or w = White
If the item number does not contain exactly five characters, or the third character is not one of the characters listed above, the program should display an appropriate message on the screen.
Explanation / Answer
#include<stdio.h>
void printColor(char* item) {
if (strlen(item) != 5)
printf("INVALID ITEM ");
else
switch(item[2]) {
case'b':
case'B':
printf("Blue ");
break;
case'r':
case'R':
printf("Red ");
break;
case'g':
case'G':
printf("Green ");
break;
case'w':
case'W':
printf("White ");
break;
default:
printf("INVALID ITEM ");
}
}
void main(){
char str[10];
while (1) {
printf("Enter item: ");
scanf("%s",str);
printColor(str);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.