Replace all vowels with next consonant in a given string

Objective: Given a string, write an algorithm to replace all the vowels with next consonant, and if last alphabet is vowel, remove it.

Example:

String input: “abcdefg”
Output: “bbcdffg”

String input: “aaaabccceee”
Output: “bbbbbccc”

String input: “aaa”
Output: “”

Approach:

  • Traverse the string right to left one character at a time.
  • Maintain a variable , last_visited_non_vowel
  • During traversal , when vowel is encountered, replace it with , last_visited_non_vowel and add to result.
  • During traversal , when consonant(non vowel) is encountered, update the last_visited_non_vowel and add to result.

Code:


Output:

Input: abcdefg, Output: bbcdffg
Input: aaaabccceee, Output: bbbbbccc
Input: aaaa, Output:

__________________________________________________
Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

%d bloggers like this: