Replace consonants with next immediate consonants alphabetically in a String

21 Jun, 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.
It 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:  
C++
Java
Python3
C#
PHP
Javascript
// C++ program of above approach#include <bits/stdc++.h>using namespace std; // Function to check if a character is// vowel or notbool isVowel(char ch){    if (ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o'        && ch != 'u')        return false;     return true;} // Function that replaces consonant with// next immediate consonant alphabeticallystring replaceConsonants(string s){    // Start traversing the string    for (int i = 0; i < s.length(); i++) {         if (!isVowel(s[i])) {             // if character is z,            // than replace it with character b            if (s[i] == 'z')                s[i] = 'b';          // if character is Z,            // than replace it with character B            else if (s[i] == 'Z')                {                    s[i] = 'B';                }            // if the alphabet is not z            else {                 // replace the element with                // next immediate alphabet                s[i] = (char)(s[i] + 1);                 // if next immediate alphabet is vowel,                // than take next 2nd immediate alphabet                // (since no two vowels occurs consecutively                // in alphabets) hence no further                // checking is required                if (isVowel(s[i]))                    s[i] = (char)(s[i] + 1);            }        }    }     return s;} // Driver codeint main(){    string s = "geeksforgeeks";     cout << replaceConsonants(s);     return 0;}
// Java program of above approachclass GFG {     // Function to check if a character is    // vowel or not    static boolean isVowel(char ch)    {        if (ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o'            && ch != 'u') {            return false;        }        return true;    }     // Function that replaces consonant with    // next immediate consonant alphabetically    static String replaceConsonants(char[] s)    {        // Start traversing the string        for (int i = 0; i < s.length; i++) {            if (!isVowel(s[i])) {                 // if character is z,                // than replace it with character b                if (s[i] == 'z') {                    s[i] = 'b';                }                 // if the alphabet is not z                else {                     // replace the element with                    // next immediate alphabet                    s[i] = (char)(s[i] + 1);                     // if next immediate alphabet is vowel,                    // than take next 2nd immediate alphabet                    // (since no two vowels occurs                    // consecutively in alphabets) hence no                    // further checking is required                    if (isVowel(s[i])) {                        s[i] = (char)(s[i] + 1);                    }                }            }        }        return String.valueOf(s);    }     // Driver code    public static void main(String[] args)    {        String s = "geeksforgeeks";        System.out.println(            replaceConsonants(s.toCharArray()));    }} // This code is contributed by Rajput-Ji
# Python3 program of above approach # Function to check if a character is# vowel or not  def isVowel(ch):     if (ch != 'a' and ch != 'e' and        ch != 'i' and ch != 'o' and            ch != 'u'):        return False     return True # Function that replaces consonant with# next immediate consonant alphabetically  def replaceConsonants(s):     # Start traversing the string    for i in range(len(s)):        if (isVowel(s[i]) == False):             # if character is z,            # than replace it with character b            if (s[i] == 'z'):                s[i] = 'b'             # if the alphabet is not z            else:                 # replace the element with                # next immediate alphabet                s[i] = chr(ord(s[i]) + 1)                 # if next immediate alphabet is vowel,                # than take next 2nd immediate alphabet                # (since no two vowels occurs consecutively                # in alphabets) hence no further                # checking is required                if (isVowel(s[i]) == True):                    s[i] = chr(ord(s[i]) + 1)     return ''.join(s)  # Driver codes = "geeksforgeeks" print(replaceConsonants(list(s))) # This code is contributed by mits
// C# program of above approachusing System; class GFG {     // Function to check if a character is    // vowel or not    static bool isVowel(char ch)    {        if (ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o'            && ch != 'u') {            return false;        }        return true;    }     // Function that replaces consonant with    // next immediate consonant alphabetically    static String replaceConsonants(char[] s)    {         // Start traversing the string        for (int i = 0; i < s.Length; i++) {            if (!isVowel(s[i])) {                 // if character is z,                // than replace it with character b                if (s[i] == 'z') {                    s[i] = 'b';                }                 // if the alphabet is not z                else {                     // replace the element with                    // next immediate alphabet                    s[i] = (char)(s[i] + 1);                     // if next immediate alphabet is vowel,                    // than take next 2nd immediate alphabet                    // (since no two vowels occurs                    // consecutively in alphabets) hence no                    // further checking is required                    if (isVowel(s[i])) {                        s[i] = (char)(s[i] + 1);                    }                }            }        }        return String.Join("", s);    }     // Driver code    public static void Main(String[] args)    {        String s = "geeksforgeeks";        Console.WriteLine(            replaceConsonants(s.ToCharArray()));    }} // This code is contributed by// 29AjayKumar
<?php// PHP program of above approach // Function to check if a character is// vowel or notfunction isVowel($ch){    if ($ch != 'a' && $ch != 'e' &&        $ch != 'i' && $ch != 'o' &&        $ch != 'u')        return false;     return true;} // Function that replaces consonant with// next immediate consonant alphabeticallyfunction replaceConsonants($s){    // Start traversing the string    for ($i = 0; $i < strlen($s); $i++)    {        if (!isVowel($s[$i]))        {             // if character is z,            // than replace it with character b            if ($s[$i] == 'z')                $s[$i] = 'b';             // if the alphabet is not z            else            {                 // replace the element with                // next immediate alphabet                $s[$i] = chr(ord($s[$i]) + 1);                 // if next immediate alphabet is vowel,                // than take next 2nd immediate alphabet                // (since no two vowels occurs consecutively                // in alphabets) hence no further                // checking is required                if (isVowel($s[$i]))                    $s[$i] = chr(ord($s[$i]) + 1);            }        }    }    return $s;} // Driver code$s = "geeksforgeeks"; echo replaceConsonants($s); // This code is contributed by mits?>
<script> // Javascript program of above approach // Function to check if a character is// vowel or notfunction isVowel(ch){    if (ch != 'a' && ch != 'e' && ch != 'i'                     && ch != 'o' && ch != 'u')        return false;     return true;} // Function that replaces consonant with// next immediate consonant alphabeticallyfunction replaceConsonants(s){    // Start traversing the string    for (var i = 0; i < s.length; i++) {         if (!isVowel(s[i])) {             // if character is z,            // than replace it with character b            if (s[i] == 'z')                s[i] = 'b';             // if the alphabet is not z            else {                 // replace the element with                // next immediate alphabet                s[i] = String.fromCharCode(s[i].charCodeAt(0) + 1);                 // if next immediate alphabet is vowel,                // than take next 2nd immediate alphabet                // (since no two vowels occurs consecutively                // in alphabets) hence no further                // checking is required                if (isVowel(s[i]))                    s[i] = String.fromCharCode(s[i].charCodeAt(0) + 1);            }        }    }     return s.join('');} // Driver codevar s = "geeksforgeeks".split('');document.write( replaceConsonants(s));  </script>
heeltgosheelt
 
Time Complexity: O(n), where n is the size of string s Auxiliary Space: O(1)
Rajput-Ji
29AjayKumar
Mithun Kumar
itsok
surindertarika1234
tajkapoor
singhh3010
vowel-consonant
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python program to check if a string is palindrome or not
Longest Palindromic Substring | Set 1
Length of the longest substring without repeating characters
Check whether two strings are anagram of each other
Top 50 String Coding Problems for Interviews
Convert string to char array in C++
Reverse words in a given string
What is Data Structure: Types, Classifications and Applications
Print all the duplicates in the input string
Reverse string in Python (6 different ways)