Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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);
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote