YouTip LogoYouTip

C Exercise Example31

# C Programming Exercise - Example 31: Determine the Day of the Week This tutorial provides a comprehensive guide to solving a classic C programming problem: determining the day of the week based on user-inputted letters. You will learn how to handle conditional branching using `switch-case` statements and manage input buffer issues in C. --- ## 1. Problem Description Write a C program that prompts the user to input the first letter of a day of the week to determine which day it is. If the first letter is identical for multiple days, the program should prompt the user to enter the second letter to make a unique determination. ### Analysis of the Days of the Week: * **M**onday (Unique) * **T**uesday / **T**hursday (Shared first letter: **T**) * **W**ednesday (Unique) * **F**riday (Unique) * **S**aturday / **S**unday (Shared first letter: **S**) ### Logic Design: Using a `switch` statement is the most elegant approach for this problem. * For unique first letters (`m`, `w`, `f`), the program can immediately print the corresponding day. * For shared first letters (`t`, `s`), the program must prompt for a second character and use nested `if` statements to determine the correct day. --- ## 2. Code Implementation Below is the complete C source code. Note the use of `getchar()` to handle the newline character left in the input buffer. ```c /** * Description: Determine the day of the week by its starting letters. * Website: YouTip (youtip.co) */ #include int main() { char i, j; printf("Please enter the first letter:\n"); scanf("%c", &i); // Consume the newline character ('\n') left in the input buffer by the previous scanf. // Without this, the next scanf would read the newline instead of the user's second letter. getchar(); switch(i) { case 'm': case 'M': printf("Monday\n"); break; case 'w': case 'W': printf("Wednesday\n"); break; case 'f': case 'F': printf("Friday\n"); break; case 't': case 'T': printf("Please enter the next letter:\n"); scanf("%c", &j); if (j == 'u' || j == 'U') { printf("Tuesday\n"); break; } if (j == 'h' || j == 'H') { printf("Thursday\n"); break; } printf("Invalid second letter!\n"); break; case 's': case 'S': printf("Please enter the next letter:\n"); scanf("%c", &j); if (j == 'a' || j == 'A') { printf("Saturday\n"); break; } if (j == 'u' || j == 'U') { printf("Sunday\n"); break; } printf("Invalid second letter!\n"); break; default: printf("Error: No day starts with that letter.\n"); break; } return 0; } ``` --- ## 3. Sample Output ### Case 1: Unique First Letter ```text Please enter the first letter: m Monday ``` ### Case 2: Shared First Letter (Requires Second Letter) ```text Please enter the first letter: s Please enter the next letter: a Saturday ``` --- ## 4. Key Considerations & Technical Insights ### 1. Handling the Input Buffer (The `getchar()` Trick) When you type a character and press **Enter**, two characters are sent to the standard input stream (`stdin`): the character itself (e.g., `'s'`) and the newline character (`'\n'`). * The first `scanf("%c", &i)` reads `'s'`. * The `'\n'` remains in the input buffer. * If you do not clear this newline, the subsequent `scanf("%c", &j)` will instantly read `'\n'` as the second letter without waiting for user input. * **Solution:** We use `getchar()` immediately after the first `scanf` to "eat" (discard) the newline character. ### 2. Case Sensitivity The original exercise only handled lowercase letters. In professional production code, it is best practice to handle both uppercase and lowercase inputs (e.g., `case 'm': case 'M':`) to make the application user-friendly and robust. ### 3. Fall-through in Switch Statements Remember to include `break` statements at the end of each case block. Omitting a `break` will cause execution to "fall through" to the next case, leading to logical errors.
← C Exercise Example32C Exercise Example30 β†’