easy
0 views

Function isNotVowel – CTS PATTERN

Fix the logical error in the isNotVowel function to correctly identify non-vowel characters

Understand the Problem

Problem Statement

You are required to fix all logical errors in the given code. The function isNotVowel(char ch) accepts a character ch as input. The function should return 1 if the character is not a vowel (a, e, i, o, u), and 0 if it is a vowel.

The function compiles fine but fails to return the correct result due to a logical error. Your task is to fix the implementation.

Constraints

  • Input character must be a valid ASCII character
  • Function should handle both uppercase and lowercase letters
  • Return value must be either 0 (is vowel) or 1 (not vowel)
  • Only the isNotVowel function needs to be corrected

Examples

Example 1
Input
'a'
Output
0
Explanation

The character 'a' is a vowel, so the function should return 0.

Example 2
Input
'b'
Output
1
Explanation

The character 'b' is not a vowel, so the function should return 1.

Example 3
Input
'E'
Output
0
Explanation

The character 'E' (uppercase) is a vowel, so the function should return 0 after converting to lowercase.

Solution

#include <ctype.h>

int isNotVowel(char ch)
{
    ch = tolower(ch);
    if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
        return 0;
    }
    return 1;
}
Time:O(1)
Space:O(1)
Approach:

The C solution converts the input character to lowercase using tolower() to handle both uppercase and lowercase vowels uniformly. Then it checks if the character matches any of the five vowels using OR (||) operators. If it matches any vowel, the function returns 0. Otherwise, it returns 1 indicating the character is not a vowel.

Visual Explanation

Loading diagram...