Count the pairs of vowels in the given string

Given a string str consisting of lowercase English alphabets, the task is to count the number of adjacent pairs of vowels.

Examples:

Input: str = “abaebio”
Output: 2
(a, e) and (i, o) are the only valid pairs.

Input: str = “aeoui”
Output: 4



Approach: Starting from the first character of the string to the second last character, increment count for every character where str[i] as well as str[i + 1] are both vowels. Print the count in the end which is the required count of pairs.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function that return true
// if character ch is a vowel
bool isVowel(char ch)
{
    switch (ch) {
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
        return true;
    default:
        return false;
    }
}
  
// Function to return the count of adjacent
// vowel pairs in the given string
int vowelPairs(string s, int n)
{
    int cnt = 0;
    for (int i = 0; i < n - 1; i++) {
  
        // If current character and the
        // character after it are both vowels
        if (isVowel(s[i]) && isVowel(s[i + 1]))
            cnt++;
    }
  
    return cnt;
}
  
// Driver code
int main()
{
    string s = "abaebio";
    int n = s.length();
    cout << vowelPairs(s, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG {
  
    // Function that return true
    // if character ch is a vowel
    static boolean isVowel(char ch)
    {
        switch (ch) {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
            return true;
        default:
            return false;
        }
    }
  
    // Function to return the count of adjacent
    // vowel pairs in the given string
    static int vowelPairs(String s, int n)
    {
        int cnt = 0;
        for (int i = 0; i < n - 1; i++) {
  
            // If current character and the
            // character after it are both vowels
            if (isVowel(s.charAt(i)) && isVowel(s.charAt(i + 1)))
                cnt++;
        }
  
        return cnt;
    }
  
    // Driver code
    public static void main(String args[])
    {
        String s = "abaebio";
        int n = s.length();
        System.out.print(vowelPairs(s, n));
    }
}

chevron_right


Python3

# Python3 implementation of the approach

# Function that return true
# if character ch is a vowel
def isVowel(ch):
if ch in [‘a’, ‘e’, ‘i’, ‘o’, ‘u’]:
return True
else:
return False

# Function to return the count of adjacent
# vowel pairs in the given string
def vowelPairs(s, n):

cnt = 0
for i in range(n – 1):

# If current character and the
# character after it are both vowels
if (isVowel(s[i]) and
isVowel(s[i + 1])):
cnt += 1

return cnt

# Driver code
s = “abaebio”
n = len(s)
print(vowelPairs(s, n))

# This code is contributed
# by mohit kumar

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System;
  
class GFG 
  
    // Function that return true 
    // if character ch is a vowel 
    static bool isVowel(char ch) 
    
        switch (ch) 
        
        case 'a'
        case 'e'
        case 'i'
        case 'o'
        case 'u'
            return true
        default
            return false
        
    
  
    // Function to return the count of adjacent 
    // vowel pairs in the given string 
    static int vowelPairs(string s, int n) 
    
        int cnt = 0; 
        for (int i = 0; i < n - 1; i++) 
        
  
            // If current character and the 
            // character after it are both vowels 
            if (isVowel(s[i]) && isVowel(s[i + 1])) 
                cnt++; 
        
  
        return cnt; 
    
  
    // Driver code 
    public static void Main() 
    
        string s = "abaebio"
        int n = s.Length; 
          
        Console.WriteLine(vowelPairs(s, n)); 
    
  
// This code is contributed by Ryuga 

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach
  
// Function that return true
// if character ch is a vowel
function isVowel($ch)
{
    if($ch == 'a' || $ch == 'e' || 
       $ch == 'i' || $ch == 'o' ||
       $ch == 'u')
        return true;
    return false;
}
  
// Function to return the count of adjacent
// vowel pairs in the given string
function vowelPairs($s, $n)
{
    $cnt = 0;
    for ($i = 0; $i < $n - 1; $i++)
    {
  
        // If current character and the
        // character after it are both vowels
        if (isVowel($s[$i]) && 
            isVowel($s[$i + 1]))
            $cnt++;
    }
    return $cnt;
}
  
// Driver code
$s = "abaebio";
$n = strlen($s);
echo vowelPairs($s, $n);
  
// This code is contributed by mits
?>

chevron_right


Output:

2


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.