C Programs in Linux-enum, switch, and strcmp

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.