I recently started to learn c Programming in Linux. Based on my own learning process, I will post a series of articles to discuss with you.
# Include <stdio. h>
# Include <string. h> // for strcmp ()
# Include <stdbool. h> // C99 feature
Enum spectrum {red, orange, yellow, green, blue, violet}; // defines the enumeration type
Const char * colors [] = {"red", "orange", "yellow ",
"Green", "blue", "violet"}; // defines an array
# Define LEN 30
Int main (void)
{
Char choice [LEN];
Enum spectrum color;
Bool color_is_found = false;
Puts ("Enter a color (empty line to quit):"); // puts () Output Function
While (gets (choice )! = NULL & choice [0]! = '\ 0') // obtain the keyboard input for logical operations
{
For (color = red; color <= violet; color ++) // traverses the enumerated type
{
If (strcmp (choice, colors [color]) = 0) // compare whether the input string exists in the array
{
Color_is_found = true;
Break;
}
}
If (color_is_found)
Switch (color) // control the process by using switch forks
{
Case red: puts ("Roses are red .");
Break;
Case orange: puts ("Poppies are orange .");
Break;
Case yellow: puts ("Sunflowers are yellow .");
Break;
Case green: puts ("Grass is green .");
Break;
Case blue: puts ("Bluebells are blue .");
Break;
Case violet: puts ("Violets are violet .");
Break;
}
Else
Printf ("I don't know about the color % s. \ n", choice );
Color_is_found = false;
Puts ("Next color, please (empty line to quit ):");
}
Puts ("Goodbye! ");
Return 0;
}