Modify string by removing vowels in between two consonants

Given a string S, comprising of only lowercase English alphabets, the task is to update the string by eliminating such vowels from the string that occur between two consonants.

Examples:

Input: bab
Output: bb

Here the letter ‘a’ is a vowel and is between two immediate consonants.
Thus, it is removed from the string, and the resultant string becomes ‘bb

Input: geeksforgeeks
Output: geeksfrgeeks
In the substring ‘for’ the alphabet ‘o’ is between two consonants ‘f’ and ‘r’.
Thus, it is removed from there and the string becomes-‘geeksfrgeeks



Approach: Initialize an empty updatedString. Given below are the steps to solve the above problem.

  • Traverse the string from left to right.
  • If the current character is a vowel, check the character before it and the character after it, if both of these are consonants, then the current vowel is a ‘Sandwiched Vowel’ and it needs to be removed from S, thus don’t append this character to A.
    Else, append the current character to A.
  • Continue the process until all the vowels in between two consonants are removed from the string

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to remove all Vowels
// in between two consonants from the string
#include <bits/stdc++.h>
using namespace std;
  
// Function to check if the character x is a vowel or not
bool isVowel(char x)
{
    if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u')
        return true;
    else
        return false;
}
  
// Returns the updated string formed after removing all
// the Sandwiched Vowels from the given string
string updateSandwichedVowels(string a)
{
    int n = a.length();
  
    // string to store the Updated String after
    // removing the Sandwiched Vowels
    string updatedString = "";
  
    // traverse the string from left to right
    for (int i = 0; i < n; i++) {
  
        // if the current character is the first or the
        // last character of the string then, this needs
        // to be appended to the updatedString, since the
        // corner alphabet irrespective of it being a vowel
        // or a consonant, is never 'Sandwiched'
        if (!i || i == n - 1) {
            updatedString += a[i];
            continue;
        }
        // Check if the current character of the string is
        // a vowel and both the previous and the next characters
        // are consonants, if so then this is a sandwiched
        // vowel, thus is ignored and not appended
        // to the updated string
        if (isVowel(a[i]) && !isVowel(a[i - 1])
            && !isVowel(a[i + 1])) {
            continue;
        }
  
        // if this character is not a sandwiched Vowel append
        // it to the updated String
        updatedString += a[i];
    }
  
    return updatedString;
}
  
// Driver Code
int main()
{
  
    string str = "geeksforgeeks";
  
    // Remove all the Sandwitched Vowels
    string updatedString = updateSandwichedVowels(str);
  
    cout << updatedString;
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to remove all 
// Vowels in between two 
// consonants from the string
import java.io.*;
import java.util.*;
import java.lang.*;
  
class GFG
{
      
// Function to check if the 
// character x is a vowel or not
static boolean isVowel(char x)
{
    if (x == 'a' || x == 'e' || 
        x == 'i' || x == 'o' || 
        x == 'u')
        return true;
    else
        return false;
}
  
// Returns the updated string 
// formed after removing all
// the Sandwiched Vowels from
// the given string
static String updateSandwichedVowels(String a)
{
    int n = a.length();
  
    // string to store the Updated 
    // String after removing the
    // Sandwiched Vowels
    String updatedString = "";
  
    // traverse the string
    // from left to right
    for (int i = 0; i < n; i++) 
    {
  
        // if the current character is 
        // the first or the last character 
        // of the string then, this needs
        // to be appended to the updatedString, 
        // since the corner alphabet irrespective 
        // of it being a vowel or a consonant, 
        // is never 'Sandwiched'
        if (i == 0 || i == n - 1
        {
            updatedString += a.charAt(i);
            continue;
        }
          
        // Check if the current character 
        // of the string is a vowel and both 
        // the previous and the next characters
        // are consonants, if so then this is 
        // a sandwiched vowel, thus is ignored 
        // and not appended to the updated string
        if (isVowel(a.charAt(i))== true &&
            isVowel(a.charAt(i - 1))== false && 
            isVowel(a.charAt(i + 1))== false)
        {
            continue;
        }
  
        // if this character is not 
        // a sandwiched Vowel append
        // it to the updated String
        updatedString += a.charAt(i);
    }
  
    return updatedString;
}
  
// Driver Code
public static void main(String[] args)
{
  
    String str = "geeksforgeeks";
  
    // Remove all the Sandwitched Vowels
    String updatedString = updateSandwichedVowels(str);
  
    System.out.print(updatedString);
  
}
}

chevron_right


Output:

geeksfrgeeks

Time Complexity: O(N) where N is the length of the input string.



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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.