Vowels Exactly Balanced
Difficulty: ⚫⚫◯◯
Tags: array, strings, counting
Hurdle: Passing this question is sufficient to pass the arrays hurdle.
Description
Write a function vowels_exactly_balanced that takes an array of strings with exactly size strings. Each string is terminated by the null terminator ('\0') character.
The function should return:
1(true) if each string contains exactly the same number of vowels0(false) if they do not
Vowels: The vowels are 'a', 'e', 'i', 'o', 'u' (both uppercase and lowercase count as vowels).
Examples
Example 1
Input:
"aaaa"
"comp"
"oooh"
"yaya"
Output: 0 (false)
Explanation: The strings have 4, 1, 3, and 2 vowels respectively. Not all the same, so return 0.
Example 2
Input:
"abc"
"!e"
"ac"
Output: 1 (true)
Explanation: Each string contains exactly 1 vowel ('a', 'e', 'a'), so return 1.
Example 3
Input:
"fgh"
"j"
"zxcvbnm"
Output: 1 (true)
Explanation: Each string contains 0 vowels, so return 1.
Example 4
Input:
"AEIOU"
"aeiou"
Output: 1 (true)
Explanation: Each string contains 5 vowels (uppercase and lowercase both count), so return 1.
Example 5
Input:
"hello"
Output: 1 (true)
Explanation: With only one string, it is always balanced, so return 1.
Function Signature
int vowels_exactly_balanced(int size, char *array[MAX_ROWS]);
Constraints
- Uppercase and lowercase vowels all count as vowels: 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'.
- You can assume there will be at least one string.
- If there is only one string, it is always balanced (return 1).
- Each string will always have a null terminator at the final position.
- Each string contains at most 100 characters.
- The array may contain any ASCII characters.
- Return
1for true,0for false. - Do not modify the given array.
- Do not call
printf(the function should only return a value).
Hints
- Count the vowels in the first string, then compare with each subsequent string.
- Remember to check both uppercase and lowercase vowels.
- A helper function to count vowels in a single string can make the code cleaner.
Testing
./vowels-exactly-balanced "aaaa" "comp" "oooh" "yaya"
0
./vowels-exactly-balanced "abc" "!e" "ac"
1
./vowels-exactly-balanced "fgh" "j" "zxcvbnm"
1
./vowels-exactly-balanced "AEIOU" "aeiou"
1
./vowels-exactly-balanced "hello"
1