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.
Examples:
Input : str = "geeksforgeeks" Output : eeeiueooeeeiu Input : str = "gfg" Output : eee
A simple approach is to compare the consonant with vowels to determine the nearest vowel. First, check if the consonant falls between two vowels. If it falls between 2 vowels then find the absolute difference between the ASCII value of consonant with the ASCII value of both vowels.
Replace it with that vowel, with which the absolute difference is minimum.
However, if the ASCII code of consonant does not fall between two vowels, then the consonant can be ‘v’, ‘w’, ‘x’, ‘y’, ‘z’. Hence, the answer is ‘u’ in this case.
Below is the implementation of the above approach:
C++
// C++ program to replace all consonants // with nearest vowels in a string #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 to replace consonant with // nearest vowels string replacingConsonants(string s) { for ( int i = 0; i < s.length(); i++) { // if, string element is vowel, // jump to next element if (isVowel(s[i])) continue ; // check if consonant lies between two vowels, // if it lies, than replace it with nearest vowel else { if (s[i] > 'a' && s[i] < 'e' ) { // here the absolute difference of // ascii value is considered if ( abs (s[i] - 'a' ) > abs (s[i] - 'e' )) s[i] = 'e' ; else s[i] = 'a' ; } else if (s[i] > 'e' && s[i] < 'i' ) { if ( abs (s[i] - 'e' ) > abs (s[i] - 'i' )) s[i] = 'i' ; else s[i] = 'e' ; } else if (s[i] > 'i' && s[i] < 'o' ) { if ( abs (s[i] - 'i' ) > abs (s[i] - 'o' )) s[i] = 'o' ; else s[i] = 'i' ; } else if (s[i] > 'o' && s[i] < 'u' ) { if ( abs (s[i] - 'o' ) > abs (s[i] - 'u' )) s[i] = 'u' ; else s[i] = 'o' ; } // when s[i] is equal to either // 'v', 'w', 'x', 'y', 'z' else if (s[i] > 'u' ) s[i] = 'u' ; } } return s; } // Driver code int main() { string s = "geeksforgeeks" ; cout << replacingConsonants(s); return 0; } |
Java
// Java program to replace all consonants // with nearest vowels in a string import java.util.*; class Solution{ // 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 to replace consonant with // nearest vowels static String replacingConsonants(String s) { for ( int i = 0 ; i < s.length(); i++) { // if, string element is vowel, // jump to next element if (isVowel(s.charAt(i))) continue ; // check if consonant lies between two vowels, // if it lies, than replace it with nearest vowel else { if (s.charAt(i) > 'a' && s.charAt(i) < 'e' ) { // here the absolute difference of // ascii value is considered if (Math.abs(s.charAt(i) - 'a' ) > Math.abs(s.charAt(i) - 'e' )) s = s.substring( 0 ,i)+ 'e' +s.substring(i+ 1 ); else s= s.substring( 0 ,i)+ 'a' +s.substring(i+ 1 ); } else if (s.charAt(i) > 'e' && s.charAt(i) < 'i' ) { if (Math.abs(s.charAt(i) - 'e' ) > Math.abs(s.charAt(i) - 'i' )) s = s.substring( 0 ,i)+ 'i' +s.substring(i+ 1 ); else s = s.substring( 0 ,i)+ 'e' +s.substring(i+ 1 ); } else if (s.charAt(i) > 'i' && s.charAt(i) < 'o' ) { if (Math.abs(s.charAt(i) - 'i' ) > Math.abs(s.charAt(i) - 'o' )) s= s.substring( 0 ,i)+ 'o' +s.substring(i+ 1 ); else s= s.substring( 0 ,i)+ 'i' +s.substring(i+ 1 ); } else if (s.charAt(i) > 'o' && s.charAt(i) < 'u' ) { if (Math.abs(s.charAt(i) - 'o' ) > Math.abs(s.charAt(i) - 'u' )) s= s.substring( 0 ,i)+ 'u' +s.substring(i+ 1 ); else s= s.substring( 0 ,i)+ 'o' +s.substring(i+ 1 ); } // when s.charAt(i) is equal to either // 'v', 'w', 'x', 'y', 'z' else if (s.charAt(i) > 'u' ) s =s.substring( 0 ,i)+ 'u' +s.substring(i+ 1 ); } } return s; } // Driver code public static void main(String args[]) { String s = "geeksforgeeks" ; System.out.print( replacingConsonants(s)); } } //contributed by Arnab Kundu |
Python3
# Python3 program to replace all consonants # with nearest vowels in a string # 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 to replace consonant # with nearest vowels def replacingConsonants(s): for i in range ( 0 , len (s)): # if, string element is vowel, # jump to next element if isVowel(s[i]): continue # check if consonant lies between two vowels, # if it lies, than replace it with nearest vowel else : if s[i] > 'a' and s[i] < 'e' : # here the absolute difference of # ascii value is considered if ( abs ( ord (s[i]) - ord ( 'a' )) > abs ( ord (s[i]) - ord ( 'e' ))): s[i] = 'e' else : s[i] = 'a' elif s[i] > 'e' and s[i] < 'i' : if ( abs ( ord (s[i]) - ord ( 'e' )) > abs ( ord (s[i]) - ord ( 'i' ))): s[i] = 'i' else : s[i] = 'e' elif (s[i] > 'i' and s[i] < 'o' ): if ( abs ( ord (s[i]) - ord ( 'i' )) > abs ( ord (s[i]) - ord ( 'o' ))): s[i] = 'o' else : s[i] = 'i' elif (s[i] > 'o' and s[i] < 'u' ): if ( abs ( ord (s[i]) - ord ( 'o' )) > abs ( ord (s[i]) - ord ( 'u' ))): s[i] = 'u' else : s[i] = 'o' # when s[i] is equal to either # 'v', 'w', 'x', 'y', 'z' elif (s[i] > 'u' ): s[i] = 'u' return ''.join(s) # Driver code if __name__ = = "__main__" : s = "geeksforgeeks" print (replacingConsonants( list (s))) # This code is contributed by Rituraj Jain |
C#
// C# program to replace all consonants // with nearest vowels in a string using System; public class Solution{ // 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 to replace consonant with // nearest vowels static String replacingConsonants(String s) { for ( int i = 0; i < s.Length; i++) { // if, string element is vowel, // jump to next element if (isVowel(s[i])) continue ; // check if consonant lies between two vowels, // if it lies, than replace it with nearest vowel else { if (s[i] > 'a' && s[i] < 'e' ) { // here the absolute difference of // ascii value is considered if (Math.Abs(s[i] - 'a' ) > Math.Abs(s[i] - 'e' )) s = s.Substring(0,i)+ 'e' +s.Substring(i+1); else s= s.Substring(0,i)+ 'a' +s.Substring(i+1); } else if (s[i] > 'e' && s[i] < 'i' ) { if (Math.Abs(s[i] - 'e' ) > Math.Abs(s[i] - 'i' )) s = s.Substring(0,i)+ 'i' +s.Substring(i+1); else s = s.Substring(0,i)+ 'e' +s.Substring(i+1); } else if (s[i] > 'i' && s[i] < 'o' ) { if (Math.Abs(s[i] - 'i' ) > Math.Abs(s[i] - 'o' )) s= s.Substring(0,i)+ 'o' +s.Substring(i+1); else s= s.Substring(0,i)+ 'i' +s.Substring(i+1); } else if (s[i] > 'o' && s[i] < 'u' ) { if (Math.Abs(s[i] - 'o' ) > Math.Abs(s[i] - 'u' )) s= s.Substring(0,i)+ 'u' +s.Substring(i+1); else s= s.Substring(0,i)+ 'o' +s.Substring(i+1); } // when s[i] is equal to either // 'v', 'w', 'x', 'y', 'z' else if (s[i] > 'u' ) s =s.Substring(0,i)+ 'u' +s.Substring(i+1); } } return s; } // Driver code public static void Main() { String s = "geeksforgeeks" ; Console.WriteLine( replacingConsonants(s)); } } // This code is contributed by PrinciRaj1992 |
Javascript
<script> // JavaScript program to replace all consonants // with nearest vowels in a string // 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 to replace consonant with // nearest vowels function replacingConsonants(s) { for ( var i = 0; i < s.length; i++) { // if, string element is vowel, // jump to next element if (isVowel(s[i])) continue ; // check if consonant lies between two vowels, // if it lies, than replace it with nearest vowel else { if (s[i] > "a" && s[i] < "e" ) { // here the absolute difference of // ascii value is considered if ( Math.abs(s[i].charCodeAt(0) - "a" .charCodeAt(0)) > Math.abs(s[i].charCodeAt(0) - "e" .charCodeAt(0)) ) s = s.substring(0, i) + "e" + s.substring(i + 1); else s = s.substring(0, i) + "a" + s.substring(i + 1); } else if (s[i] > "e" && s[i] < "i" ) { if ( Math.abs(s[i].charCodeAt(0) - "e" .charCodeAt(0)) > Math.abs(s[i].charCodeAt(0) - "i" .charCodeAt(0)) ) s = s.substring(0, i) + "i" + s.substring(i + 1); else s = s.substring(0, i) + "e" + s.substring(i + 1); } else if (s[i] > "i" && s[i] < "o" ) { if ( Math.abs(s[i].charCodeAt(0) - "i" .charCodeAt(0)) > Math.abs(s[i].charCodeAt(0) - "o" .charCodeAt(0)) ) s = s.substring(0, i) + "o" + s.substring(i + 1); else s = s.substring(0, i) + "i" + s.substring(i + 1); } else if (s[i] > "o" && s[i] < "u" ) { if ( Math.abs(s[i].charCodeAt(0) - "o" .charCodeAt(0)) > Math.abs(s[i].charCodeAt(0) - "u" .charCodeAt(0)) ) s = s.substring(0, i) + "u" + s.substring(i + 1); else s = s.substring(0, i) + "o" + s.substring(i + 1); } // when s[i] is equal to either // 'v', 'w', 'x', 'y', 'z' else if (s[i] > "u" ) s = s.substring(0, i) + "u" + s.substring(i + 1); } } return s; } // Driver code var s = "geeksforgeeks" ; document.write(replacingConsonants(s)); </script> |
eeeiueooeeeiu
Complexity Analysis:
- Time Complexity: O(n), where n is the size of string s
- Auxiliary Space: O(1)
A better approach is to make an array of size 26 that stores nearest vowel for every character.
Implementation:
C++
// C++ program to replace all consonants // with nearest vowels in a string #include <bits/stdc++.h> using namespace std; // Function to replace consonant with // nearest vowels string replacingConsonants(string s) { char nVowel[] = "aaaeeeeiiiiioooooouuuuuuuu" ; for ( int i = 0; i < s.length(); i++) s[i] = nVowel[s[i] - 'a' ]; return s; } // Driver code int main() { string s = "geeksforgeeks" ; cout << replacingConsonants(s); return 0; } |
Java
// Java program to replace all consonants // with nearest vowels in a string import java.util.*; class solution { // Function to replace consonant with // nearest vowels static String replacingConsonants(String s) { String str = "aaaeeeeiiiiioooooouuuuuuuu" ; char [] st = s.toCharArray(); for ( int i = 0 ; i < s.length(); i++) { int index = st[i]- 'a' ; st[i] = str.charAt(index); } String str1 = new String(st); return str1; } // Driver code public static void main(String arr[]) { String s = "geeksforgeeks" ; System.out.println(replacingConsonants(s)); } } // This code is contributed by Surendra_Gangwar |
Python3
# Python3 program to replace all consonants # with nearest vowels in a string # Function to replace consonant with # nearest vowels def replacingConsonants(s): nVowel = "aaaeeeeiiiiioooooouuuuuuuu" for i in range ( 0 , len (s)): s = s.replace(s[i], nVowel[ ord (s[i]) - 97 ]) return s # Driver code s = "geeksforgeeks" ; print (replacingConsonants(s)); # This code is contributed by # archana_kumari. |
C#
// C# program to replace all consonants // with nearest vowels in a string using System; public class solution{ // Function to replace consonant with // nearest vowels static String replacingConsonants(String s) { String str = "aaaeeeeiiiiioooooouuuuuuuu" ; char [] st = s.ToCharArray(); for ( int i = 0; i < s.Length; i++) { int index = st[i]- 'a' ; st[i] = str[index]; } String str1 = new String(st); return str1; } // Driver code public static void Main() { String s = "geeksforgeeks" ; Console.WriteLine(replacingConsonants(s)); } } // This code is contributed by 29AjayKumar |
Javascript
<script> // Javascript program to replace all consonants // with nearest vowels in a string // Function to replace consonant with // nearest vowels function replacingConsonants(s) { var nVowel = "aaaeeeeiiiiioooooouuuuuuuu" ; for ( var i = 0; i < s.length; i++) s[i] = nVowel[s[i].charCodeAt(0) - 'a' .charCodeAt(0)]; return s.join( '' ); } // Driver code var s = "geeksforgeeks" .split( '' ); document.write( replacingConsonants(s)); </script> |
eeeiueooeeeiu
Complexity Analysis:
- Time Complexity: O(n), where n is the size of string s
- Auxiliary Space: O(26)
Please Login to comment...