This method aims to replace a string of consonants with the closest vowels in the alphabet (also known as lowercase Latin letters). If two vowels are equally close, we can replace them with the first vowel in those letters.
Let’s look at some input scenarios -
Suppose we have a string, such as "ebgkjasjd", and now we need to replace all occurrences of consonants in the string with the nearest vowels.
Input = "ebgkjasjd"; Result = ebgkjasjd eaeiiauie
Replace element 'b' with 'a' since it is the nearest vowel. We could have replaced element 'g' with 'e' or 'i' since they are both equally recent, but we choose to replace it with 'e' since it came first. The same goes for all other elements in the string.
Assume that if the input string does not contain any vowels, for example, consider a string: "sdfhgtykl", the resulting output is -
Input = "sdfhgtykl"; Result = sdfhgtykl ueeieuuii
For the first element 's', the vowel 'u' is the closest, so replace 's' with 'u'. Like all other elements in the array, they are consonants. This situation also yields the worst-case complexity of the method.
Traverse the input string starting from the starting index.
If a consonant is encountered, the number of letters from the consonant to the two nearest vowels on the left and right will be counted. Choose vowels with fewer letters.
This process will be repeated until all consonants in the string have been traversed.
The output obtained is the replaced string.
The following is code implemented in C to replace all consonants in a string with the 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; }
“ibgshzjbsh” “iaeuiuiaui”
However, a better approach is to use an array and store the closest character answer for each character instead of using lots of if-else conditions.
#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; }
iaeuiuiaui
We can replace an element by finding it and replacing it with the closest character. The time complexity of traversing a string is O(n). The second method is easier to understand and code, and less cumbersome. The time complexity is also O(n) since we only iterate through the string once.
The above is the detailed content of Replace all consonants in string with nearest vowel using C++ program. For more information, please follow other related articles on the PHP Chinese website!