easy
1 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 ch can 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
Y
Output
Yes
Explanation

The character 'Y' matches the condition for printing 'Yes'

Example 2
Input
n
Output
No
Explanation

The character 'n' matches the condition for printing 'No'

Example 3
Input
A
Output
Invalid
Explanation

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:

  1. if(ch=='y' || ch=='Y') - Checks if the character is either lowercase 'y' or uppercase 'Y'
  2. If true, prints "Yes" followed by newline
  3. else if (ch=='n' || ch=='N') - If first condition is false, checks for 'n' or 'N'
  4. If true, prints "No" followed by newline
  5. else - For all other characters, prints "Invalid" followed by newline

Visual Explanation

Loading diagram...