Replace All Consonants with Nearest Vowels In a String using C++ program


The method is devised to replace a string of consonants with the closest vowels from the alphabet (also known as lowercase Latin letters). If two vowels are equally close, we can replace them with the first ones in these letters.

Let us look at some input scenarios −

Suppose we have a string such as “ebgkjasjd,” and now we need to change all the present consonants with the nearest vowels in a string.

Input = "ebgkjasjd";
Result =
ebgkjasjd
eaeiiauie

Taking element ‘b,’ we can replace it with ‘a’ since that is the nearest vowel. We could replace element ‘g’ with ‘e’ or ‘i’ because both are equally nearest, but we replace it with ‘e’ as it comes first. It is similar for all other elements in the string.

Suppose if the input string does not have any vowels present, for example, consider a string: “sdfhgtykl”, the resultant output would be obtained as −

Input = "sdfhgtykl";
Result =
sdfhgtykl
ueeieuuii

For the first element ‘s’, the vowel ‘u’ is the closest therefore ‘s’ is replaced with ‘u’. It is similar to all the other elements in the array as they’re all consonants. This case also generates the worst case complexity of this method.

Algorithm

  • Traverse through the input string from the starting index.

  • If a consonant in encountered, the amount of letters from the consonant to two nearest vowels from the left and right are calculated. The vowel bearing less letters between will be selected.

  • The process is repeated until all the consonants in the string are traversed.

  • Output obtained is the replaced string.

Example

Following is the C++ implementation to replace all the consonants in a string with nearest vowels in the alphabet −

#include <iostream> using namespace std; void solve(string& s) { for(char &ch : s) { if(ch>'a' && ch<'e') { if(ch-'a' <= 'e'-ch) ch='a'; else ch='e'; } else if(ch>'e' && ch<'i') { if(ch-'e' <= 'i'-ch) ch='e'; else ch='i'; } else if(ch>'i' && ch<'o') { if(ch-'i' <= 'o'-ch) ch='i'; else ch='o'; } else if(ch>'o' && ch<'u') { if(ch-'o' <= 'u'-ch) ch='o'; else ch='u'; } else if(ch>'u') { ch = 'u'; } } } int main() { string s = "ibgshzjbsh"; cout << s << "\n"; solve(s); cout << s; return 0; }

Output

“ibgshzjbsh”
“iaeuiuiaui”

Example

But, instead of using a lot of if-else conditions, a better approach would be to use an array and store the nearest character answer for each character.

#include <iostream> #include <vector> using namespace std; string solve(string s) { string hash = "aaaeeeeiiiiioooooouuuuuuuu"; for (int i=0;i<s.size();i++) { s[i] = hash[s[i]-'a']; } return s; } int main() { string s = "ibgshzjbsh"; cout << solve(s); return 0; }

Output

iaeuiuiaui

Conclusion

We can replace the elements by finding and replacing the element with the nearest character. Traversing the string once gives us O(n) time complexity. The second approach is much easier and less cumbersome to understand and code. The time complexity for this also is O(n) as we are traversing the string once.

Updated on: 10-Aug-2022

884 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements