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 a , it will be replaced by b . Another example, let's say we have to replace character d , the next immediate consonant is f , hence d will be replaced by f .
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:
C++
// 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 alphabetically
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 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 code
int main()
{
string s = "geeksforgeeks";
cout << replaceConsonants(s);
return 0;
}
Java
// 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 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
# 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 code
s = "geeksforgeeks"
print(replaceConsonants(list(s)))
# This code is contributed by mits
C#
// 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 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
// 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 alphabetically
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
?>
JavaScript
<script>
// Javascript 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 alphabetically
function 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 code
var s = "geeksforgeeks".split('');
document.write( replaceConsonants(s));
</script>
Complexity Analysis:
- Time Complexity: O(n), where n is the size of string s
- Auxiliary Space: O(1)
Similar Reads
Replace all consonants with nearest vowels in a string Given a string with lowercase English alphabets. The task is to replace all the consonants in the string with the nearest vowels. If a consonant is near to two vowels then replace it with the one that comes first in English alphabets. Note: Vowels already present in the string must be left as it is.
10 min read
Count of possible Strings by replacing consonants with nearest vowel Given a string str consisting of N letters, the task is to find the total number of strings that can be generated by replacing each consonant with the vowel closest to it in the English alphabet. Examples: Input: str = "code"Output: 2Explanation: Str = "code" has two consonant c and d. Closest vowel
4 min read
Count consonants in a string (Iterative and recursive methods) Given a string, count total number of consonants in it. A consonant is an English alphabet character that is not vowel (a, e, i, o and u). Examples of constants are b, c, d, f, and g. Examples : Input : abc de Output : 3 There are three consonants b, c and d. Input : geeksforgeeks portal Output : 12
7 min read
No. of vowels and consonants in a given string in PL/SQL Prerequisite - PL/SQL introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Given a string and the task is to find the number of vo
2 min read
Replace every consonant sequence with its length in the given string Given a string str consisting of lowercase characters. The task is to replace the consecutive sequence of consonants with their length.Examples: Input: str = "abcdeiop" Output: a3eio1 Given string contains 2 consonant sequences "bcd" and "p" with lengths 3 and 1.Input: str = "abecidofu" Output: a1e1
7 min read
Modify the string by swapping continuous vowels or consonants Given a string str. The task is to modify the string by swapping two adjacent characters if both of them are vowels or both of them are consonants. Examples: Input: str = "geeksforgeeks" Output: geesfkogreesk The alphabets 'e' and 'e' in geeksforgeeks are vowels so they are swapped so the string bec
6 min read