Replace consonants with next immediate consonants alphabetically in a String

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:



Below is the implementation of the above program:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program of above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to check if a character is
// vowel or not
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 alphabatically
string 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 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 code
int main()
{
    string s = "geeksforgeeks";
  
    cout << replaceConsonants(s);
  
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program of above approach
class 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 alphabatically
    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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 alphabatically
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 code
s = "geeksforgeeks";
  
print(replaceConsonants(list(s)));
  
# This code is contributed by mits
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program of above approach 
using 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 alphabatically 
    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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program of above approach
  
// Function to check if a character is
// vowel or not
function 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 alphabatically
function 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
?>
chevron_right

Output:
heeltgosheelt

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




Article Tags :
Practice Tags :