Function toggleVowelCase – CTS PATTERN
Fix the logical error in the toggleVowelCase function to correctly toggle the case of vowels in a string
Understand the Problem
Problem Statement
You are required to fix the logical error in the given code. You can click on Run button anytime to check the compilation/execution status of the program. You can use printf to debug your code. The submitted code should be logically/syntactically correct and pass all the test cases. Do not write the main() function as it is not required.
Code Approach: For this question, you will need to correct the given implementation. We do not expect you to modify the approach or incorporate any additional library methods.
The function toggleVowelCase(char str[]) accepts a string str as the input. The function is supposed to print the toggle case of all the vowels in str.
The function compiles fine but fails to print the desired results due to logical errors.
Your task is to fix the program so that it passes all test cases.
Constraints
- String length can be up to 1000 characters
- String may contain alphabetic characters (both uppercase and lowercase)
- String may contain special characters and digits which should remain unchanged
- Vowels are: a, e, i, o, u (case-insensitive)
- Output should preserve the order of characters
- Non-vowel characters should remain unchanged
Examples
thAnkyouthankyOUThe uppercase 'A' becomes lowercase 'a', and the lowercase 'o' and 'u' become uppercase 'O' and 'U'.
Hello WorldhEllO wOrldThe uppercase 'E' and 'O' become lowercase 'e' and 'o', while the lowercase 'e', 'o' become uppercase 'E' and 'O'.
AEIOUaeiouAll uppercase vowels are converted to their lowercase equivalents.
Solution
#include <stdio.h>
#include <ctype.h>
void toggleVowelCase(char str[]) {
for(int index = 0; str[index] != '\0'; index++) {
char currentChar = str[index];
// Check if the character is a vowel (case-insensitive)
if (currentChar == 'a' || currentChar == 'e' || currentChar == 'i' ||
currentChar == 'o' || currentChar == 'u' ||
currentChar == 'A' || currentChar == 'E' || currentChar == 'I' ||
currentChar == 'O' || currentChar == 'U') {
// Toggle the case of the vowel
if (islower(currentChar)) {
printf("%c", toupper(currentChar));
} else {
printf("%c", tolower(currentChar));
}
} else {
// Print non-vowel characters as-is
printf("%c", currentChar);
}
}
}Step-by-step explanation:
- Include necessary headers: We include
<stdio.h>for printf and<ctype.h>for islower(), toupper(), and tolower() functions - Function signature: The function takes a character array (string) as parameter
- Loop through string: We iterate through each character until we reach the null terminator (\0)
- Vowel detection: We check if the current character is any of the vowels (a, e, i, o, u) in either case
- Case toggling: If it's a vowel, we check if it's lowercase using islower(). If so, we convert to uppercase with toupper(). Otherwise, we convert to lowercase with tolower()
- Non-vowel handling: If the character is not a vowel, we print it unchanged