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:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: