Home > Backend Development > C++ > Replace all consonants in string with nearest vowel using C++ program

Replace all consonants in string with nearest vowel using C++ program

WBOY
Release: 2023-09-11 10:17:02
forward
1010 people have browsed it

Replace all consonants in string with nearest vowel using C++ program

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
Copy after login

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
Copy after login

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.

algorithm

  • 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.

Example

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;
}
Copy after login

Output

“ibgshzjbsh”
“iaeuiuiaui”
Copy after login

Example

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;
}
Copy after login

Output

iaeuiuiaui
Copy after login

in conclusion

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template