C++ code to find corrected text after double vowel removal


Suppose we have a string S with n character. On a text editor, there is a strange rule. The word corrector of this text editor works in such a way that as long as there are two consecutive vowels in the word, it deletes the first vowel in a word. If there are no two consecutive vowels in the word, it is considered to be correct. We have to find corrected word from S. Here vowels are 'a', 'e', 'i' 'o', 'u' and 'y'.

So, if the input is like S = "poor", then the output will be "por".

Steps

To solve this, we will follow these steps −

n := size of S
t := "aeiouy"
for initialize i := 1, when i < n, update (increase i by 1), do:
   if S[i] is in t and S[i - 1] is in t, then:
      delete ith character from S
      (decrease i by 1)
return S

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
string solve(string S){
   int n = S.size();
   string t = "aeiouy";
   for (int i = 1; i < n; i++){
      if (t.find(S[i]) != -1 && t.find(S[i - 1]) != -1){
         S.erase(i, 1);
         i--;
      }
   }
   return S;
}
int main(){
   string S = "poor";
   cout << solve(S) << endl;
}

Input

"poor"

Output

por

Updated on: 29-Mar-2022

117 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements