Count Same Vowel Pairs
Difficulty: ⚫⚫◯◯
Tags: array, strings, counting, pairs
Hurdle: Passing this question is sufficient to pass the arrays hurdle.
Description
Write a function count_same_vowel_pairs that takes an array of strings with exactly size strings. Each string is terminated by the null terminator ('\0') character.
The function should return the number of adjacent row pairs that have the same number of vowels.
Adjacent rows means consecutive rows: (row 0, row 1), (row 1, row 2), (row 2, row 3), etc.
Vowels: a, e, i, o, u (and their uppercase versions A, E, I, O, U)
Examples
Example 1
"apple" // a, e -> 2 vowels
"hello" // e, o -> 2 vowels
"world" // o -> 1 vowel
"cat" // a -> 1 vowel
"beautiful" // e, a, u, i, u -> 5 vowels
Vowel counts: [2, 2, 1, 1, 5]
Adjacent pairs:
- Row 0 and Row 1: 2 vs 2 → SAME ✓
- Row 1 and Row 2: 2 vs 1 → DIFFERENT
- Row 2 and Row 3: 1 vs 1 → SAME ✓
- Row 3 and Row 4: 1 vs 5 → DIFFERENT
Result: 2
Example 2: All Same
"cat" // 1 vowel
"dog" // 1 vowel
"pig" // 1 vowel
"sun" // 1 vowel
"bed" // 1 vowel
Vowel counts: [1, 1, 1, 1, 1]
Result: 4 (all 4 adjacent pairs match)
Example 3: None Match
"apple" // 2 vowels
"cat" // 1 vowel
"hello" // 2 vowels
"dog" // 1 vowel
"ice" // 2 vowels
Vowel counts: [2, 1, 2, 1, 2]
Result: 0 (alternating pattern, no adjacent pairs match)
Example 4: Zero Vowels Can Match
"xyz" // 0 vowels
"zzz" // 0 vowels
"cat" // 1 vowel
"bcd" // 0 vowels
"fgh" // 0 vowels
Vowel counts: [0, 0, 1, 0, 0]
Result: 2 (row 0-1 match with 0, row 3-4 match with 0)
Example 5: Single String
"hello" // 2 vowels
Result: 0 (no adjacent pairs possible)
Function Signature
int count_same_vowel_pairs(int size, char *array[MAX_ROWS]);
Constants
#define MAX_ROWS 100
Constraints
- Uppercase and lowercase vowels all count as vowels.
- You can assume there will be at least one string.
- If there is only one string, there are no adjacent pairs, so return 0.
- Rows with zero vowels can still form matching pairs (0 == 0).
- Each string contains at most 100 characters.
- Do not modify the array provided.
- Do not call
scanf,getchar, orfgets. - Do not call
printf(the function should only return a value).
Hints
- Create a helper function to count vowels in a single string.
- Compare adjacent rows: if
count_vowels(array[i]) == count_vowels(array[i+1]), increment your counter. - Remember: with
sizestrings, there aresize - 1adjacent pairs.
Testing
./count-same-vowel-pairs apple hello world cat beautiful
2
./count-same-vowel-pairs cat dog pig sun bed
4
./count-same-vowel-pairs apple cat hello dog ice
0
./count-same-vowel-pairs xyz zzz cat bcd fgh
2
./count-same-vowel-pairs hello
0