VOOZH about

URL: https://www.geeksforgeeks.org/dsa/replace-consonants-with-next-immediate-consonants-alphabetically-in-a-string/

⇱ Replace consonants with next immediate consonants alphabetically in a String - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Replace consonants with next immediate consonants alphabetically in a String

Last Updated : 11 Oct, 2022

Given a string which contains lowercase English alphabets. The task is to replace each consonant with the next immediate consonant that comes in English alphabets.
Let's say we have to replace character , it will be replaced by . Another example, let's say we have to replace character , the next immediate consonant is , hence will be replaced by .

Note: If the character is 'z', then look circularly in English alphabets for the next consonant, i.e. replace it with 'b'.

Examples

Input : str = "geeksforgeeks"
Output : heeltgosheelt

Input : str = "gfg"
Output : hgh

Approach: 

  • Iterate the string elements from left to right.
  • If the string element is consonant, then check the next immediate alphabet of this element.
  • If the next immediate alphabet is a consonant, then replace it with the this alphabet. If it is a vowel, then replace the string element with 2nd immediate alphabet as there are no consecutive vowels in English alphabets.

Below is the implementation of the above program: 


Output
heeltgosheelt

Complexity Analysis:

  • Time Complexity: O(n), where n is the size of string s 
  • Auxiliary Space: O(1)
Comment
Article Tags: