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:
C++
#include <bits/stdc++.h>
using namespace std;
bool isVowel( char ch)
{
if (ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o'
&& ch != 'u' )
return false ;
return true ;
}
string replaceConsonants(string s)
{
for ( int i = 0; i < s.length(); i++) {
if (!isVowel(s[i])) {
if (s[i] == 'z' )
s[i] = 'b' ;
else if (s[i] == 'Z' )
{
s[i] = 'B' ;
}
else {
s[i] = ( char )(s[i] + 1);
if (isVowel(s[i]))
s[i] = ( char )(s[i] + 1);
}
}
}
return s;
}
int main()
{
string s = "geeksforgeeks" ;
cout << replaceConsonants(s);
return 0;
}
|
Java
class GFG {
static boolean isVowel( char ch)
{
if (ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o'
&& ch != 'u' ) {
return false ;
}
return true ;
}
static String replaceConsonants( char [] s)
{
for ( int i = 0 ; i < s.length; i++) {
if (!isVowel(s[i])) {
if (s[i] == 'z' ) {
s[i] = 'b' ;
}
else {
s[i] = ( char )(s[i] + 1 );
if (isVowel(s[i])) {
s[i] = ( char )(s[i] + 1 );
}
}
}
}
return String.valueOf(s);
}
public static void main(String[] args)
{
String s = "geeksforgeeks" ;
System.out.println(
replaceConsonants(s.toCharArray()));
}
}
|
Python3
def isVowel(ch):
if (ch ! = 'a' and ch ! = 'e' and
ch ! = 'i' and ch ! = 'o' and
ch ! = 'u' ):
return False
return True
def replaceConsonants(s):
for i in range ( len (s)):
if (isVowel(s[i]) = = False ):
if (s[i] = = 'z' ):
s[i] = 'b'
else :
s[i] = chr ( ord (s[i]) + 1 )
if (isVowel(s[i]) = = True ):
s[i] = chr ( ord (s[i]) + 1 )
return ''.join(s)
s = "geeksforgeeks"
print (replaceConsonants( list (s)))
|
C#
using System;
class GFG {
static bool isVowel( char ch)
{
if (ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o'
&& ch != 'u' ) {
return false ;
}
return true ;
}
static String replaceConsonants( char [] s)
{
for ( int i = 0; i < s.Length; i++) {
if (!isVowel(s[i])) {
if (s[i] == 'z' ) {
s[i] = 'b' ;
}
else {
s[i] = ( char )(s[i] + 1);
if (isVowel(s[i])) {
s[i] = ( char )(s[i] + 1);
}
}
}
}
return String.Join( "" , s);
}
public static void Main(String[] args)
{
String s = "geeksforgeeks" ;
Console.WriteLine(
replaceConsonants(s.ToCharArray()));
}
}
|
PHP
<?php
function isVowel( $ch )
{
if ( $ch != 'a' && $ch != 'e' &&
$ch != 'i' && $ch != 'o' &&
$ch != 'u' )
return false;
return true;
}
function replaceConsonants( $s )
{
for ( $i = 0; $i < strlen ( $s ); $i ++)
{
if (!isVowel( $s [ $i ]))
{
if ( $s [ $i ] == 'z' )
$s [ $i ] = 'b' ;
else
{
$s [ $i ] = chr (ord( $s [ $i ]) + 1);
if (isVowel( $s [ $i ]))
$s [ $i ] = chr (ord( $s [ $i ]) + 1);
}
}
}
return $s ;
}
$s = "geeksforgeeks" ;
echo replaceConsonants( $s );
?>
|
Javascript
<script>
function isVowel(ch)
{
if (ch != 'a' && ch != 'e' && ch != 'i'
&& ch != 'o' && ch != 'u' )
return false ;
return true ;
}
function replaceConsonants(s)
{
for ( var i = 0; i < s.length; i++) {
if (!isVowel(s[i])) {
if (s[i] == 'z' )
s[i] = 'b' ;
else {
s[i] = String.fromCharCode(s[i].charCodeAt(0) + 1);
if (isVowel(s[i]))
s[i] = String.fromCharCode(s[i].charCodeAt(0) + 1);
}
}
}
return s.join( '' );
}
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...