easy
0 views
Function printYesOrNo – CTS PATTERN
Print 'Yes' for 'Y'/'y', 'No' for 'N'/'n', or 'Invalid' for any other character
Understand the Problem
Problem Statement
The function printYesOrNo(char ch) accepts a character ch as input. The function should print:
- "Yes" if the character is 'Y' or 'y'
- "No" if the character is 'N' or 'n'
- "Invalid" for any other character
Implement the missing logic in the function to handle these cases correctly.
Constraints
- The input character
chcan be any ASCII character - The function should only print the result, not return a value
- Case sensitivity is handled - both uppercase and lowercase 'Y'/'N' are valid
- Only single character input is expected
Examples
Example 1
Input
YOutput
YesExplanation
The character 'Y' matches the condition for printing 'Yes'
Example 2
Input
nOutput
NoExplanation
The character 'n' matches the condition for printing 'No'
Example 3
Input
AOutput
InvalidExplanation
The character 'A' doesn't match any of the valid conditions, so 'Invalid' is printed
Solution
void printYesOrNo(char ch)
{
if(ch=='y' || ch=='Y')
printf("Yes\n");
else if (ch=='n' || ch=='N')
printf("No\n");
else
printf("Invalid\n");
}Time:O(1) - Constant time, as it involves only character comparisons
Space:O(1) - No additional space required beyond the input parameter
Approach:
Step-by-step explanation:
if(ch=='y' || ch=='Y')- Checks if the character is either lowercase 'y' or uppercase 'Y'- If true, prints "Yes" followed by newline
else if (ch=='n' || ch=='N')- If first condition is false, checks for 'n' or 'N'- If true, prints "No" followed by newline
else- For all other characters, prints "Invalid" followed by newline
Visual Explanation
Loading diagram...