easy
1 views

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

Example 1
Input
lion
Output
1
Explanation

In 'lion', the vowels are 'i' and 'o' which appear consecutively at positions 1 and 2, so there is 1 consecutive vowel pair.

Example 2
Input
arealiouas
Output
4
Explanation

In '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;
}
Time:O(n) - where n is the length of the string
Space:O(1) - only using constant extra space for variables
Approach:

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

Visual Explanation

Loading diagram...