
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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.
- Related Articles
- C Program to count vowels, digits, spaces, consonants using the string concepts
- C# Program to count number of Vowels and Consonants in a string
- Moving vowels and consonants using JavaScript
- Java Program to count all vowels in a string
- C++ Program to Find the Number of Vowels, Consonants, Digits and White Spaces in a String
- C# program to replace all spaces in a string with ‘%20’
- Python Program to Replace all Occurrences of ‘a’ with $ in a String
- Program to sort all vowels at beginning then the consonants, are in sorted order in Python
- How to count number of vowels and consonants in a string in C Language?
- Java Program to Count the Number of Vowels and Consonants in a Sentence
- Swift Program to Count the Number of Vowels and Consonants in a Sentence
- Haskell Program to Count the Number of Vowels and Consonants in a Sentence
- Kotlin Program to Count the Number of Vowels and Consonants in a Sentence
- Alternating Vowels and Consonants in C/C++
- Frequency of vowels and consonants in JavaScript
