Consecutive Vowels Count
Count the number of times a vowel is followed by another vowel in a given string
Understand the Problem
Problem Statement
Given a string S1, print the count C which represents the number of times a vowel is followed by another vowel in the string S1.
Constraints
- String length is between 1 and 1000 characters
- String contains only alphabetic characters
- Case insensitive (both uppercase and lowercase vowels count)
Examples
lion1In 'lion', the vowels are 'i' and 'o' which appear consecutively at positions 1 and 2, so there is 1 consecutive vowel pair.
arealiouas4In 'arealiouas', the consecutive vowel pairs are: 'ea' (positions 1-2), 'io' (positions 4-5), 'ou' (positions 6-7), and 'ua' (positions 7-8), totaling 4 pairs.
Solution
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int isVowel(char c) {
c = tolower(c);
return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}
int main() {
char s[1001];
fgets(s, sizeof(s), stdin);
// Remove newline if present
int len = strlen(s);
if (s[len-1] == '\n') {
s[len-1] = '\0';
len--;
}
int count = 0;
for (int i = 0; i < len - 1; i++) {
if (isVowel(s[i]) && isVowel(s[i+1])) {
count++;
}
}
printf("%d\n", count);
return 0;
}C Solution Explanation:
1. isVowel() function: Takes a character, converts it to lowercase, and checks if it's a vowel
2. main() function: Reads the string using fgets() to handle spaces if any
3. Removes trailing newline character from input
4. Iterates through the string checking consecutive pairs
5. Uses the helper function to verify both characters are vowels
6. Prints the final count