Given string str of size N, which contains lowercase English alphabets. The task is to replace each vowel with the next immediate vowel lexicographically, i.e.,
‘a’ will be replaced by ‘e’,
‘e’ will be replaced by ‘i’,
‘i’ will be replaced by ‘o’,
‘o’ will be replaced by ‘u’,
‘u’ will be replaced by ‘a’.
Examples:
Input: str = “geeksforgeeks”
Output: giiksfurgiiks
Explaination:
e is replace by i
o is replace by u
So final string will be “giiksfurgiiks”.Input: str = “gfg”
Output: gfg
Approach: We will create a Hashing of size 5 to store all vowels so that the replacement can be done easily for each vowel.
- Create a map and store all vowels.
- Iterate the string elements from left to right.
- It the string element is a vowel, then change it to the next vowels.
- Finally, print the final string.
Here is the implementation of the above approach:
C++14
// C++ program to convert all the vowels in// in the string to the next vowel#include <bits/stdc++.h>using namespace std;// Function to replace every vowel// with next vowel lexicographicallystring print_next_vovel_string(string str){ // Storing the vowels in the map with // custom numbers showing their index map<char, int> m; m['a'] = 0; m['e'] = 1; m['i'] = 2; m['o'] = 3; m['u'] = 4; char arr[5] = { 'a', 'e', 'i', 'o', 'u' }; int N = str.length(); // Iterate over the string for (int i = 0; i < N; i++) { char c = str[i]; // If the current character is a vowel // Find the index in Hash and // Replace it with next vowel from Hash if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { int index = m + 1; int newindex = index % 5; str[i] = arr[newindex]; } } return str;}// Driver functionint main(){ string str = "geeksforgeeks"; cout << print_next_vovel_string(str); return 0;} |
Java
// Java program to convert all the vowels in// in the String to the next vowelimport java.util.*;class GFG{// Function to replace every vowel// with next vowel lexicographicallystatic String print_next_vovel_String(char []str){ // storing the vowels in the map with // custom numbers showing their index HashMap<Character, Integer> m = new HashMap<Character, Integer>(); m.put('a', 0); m.put('e', 1); m.put('i', 2); m.put('o', 3); m.put('u', 4); char arr[] = { 'a', 'e', 'i', 'o', 'u' }; int N = str.length; // Iterate over the String for(int i = 0; i < N; i++) { char c = str[i]; // If the current character is a vowel // Find the index in Hash and // Replace it with next vowel from Hash if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { int index = m.get(c) + 1; int newindex = index % 5; str[i] = arr[newindex]; } } return String.valueOf(str);}// Driver codepublic static void main(String[] args){ String str = "geeksforgeeks"; System.out.print(print_next_vovel_String( str.toCharArray()));}}// This code is contributed by Amit Katiyar |
Python3
# Python3 program to convert # all the vowels in the string # to the next vowel# Function to replace every vowel# with next vowel lexicographicallydef print_next_vovel_string(st): # Storing the vowels in # the map with custom # numbers showing their index m = {} m['a'] = 0 m['e'] = 1 m['i'] = 2 m['o'] = 3 m['u'] = 4 arr = ['a', 'e', 'i', 'o', 'u'] N = len(st) # Iterate over the string for i in range (N): c = st[i] # If the current character # is a vowel # Find the index in Hash # and Replace it with next # vowel from Hash if (c == 'a' or c == 'e' or c == 'i'or c == 'o' or c == 'u'): index = m[st[i]] + 1 newindex = index % 5 st = st.replace(st[i], arr[newindex], 1) return st# Driver functionif __name__ == "__main__": st = "geeksforgeeks" print (print_next_vovel_string(st)) # This code is contributed by Chitranayal |
C#
// C# program to convert all the vowels in// in the String to the next vowelusing System;using System.Collections.Generic;class GFG{// Function to replace every vowel// with next vowel lexicographicallystatic String print_next_vovel_String(char []str){ // Storing the vowels in the map with // custom numbers showing their index Dictionary<char, int> m = new Dictionary<char, int>(); m.Add('a', 0); m.Add('e', 1); m.Add('i', 2); m.Add('o', 3); m.Add('u', 4); char []arr = { 'a', 'e', 'i', 'o', 'u' }; int N = str.Length; // Iterate over the String for(int i = 0; i < N; i++) { char c = str[i]; // If the current character is a vowel // Find the index in Hash and // Replace it with next vowel from Hash if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { int index = m + 1; int newindex = index % 5; str[i] = arr[newindex]; } } return String.Join("", str);}// Driver codepublic static void Main(String[] args){ String str = "geeksforgeeks"; Console.Write(print_next_vovel_String( str.ToCharArray()));}}// This code is contributed by Amit Katiyar |
giiksfurgiiks
Time Complexity: O (N)
Auxiliary Space: O (1)
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.
Recommended Posts:
- Minimize cost to replace all the vowels of a given String by a single vowel
- Lexicographically first alternate vowel and consonant string
- Find a string such that every character is lexicographically greater than its immediate next character
- Replace every array element by multiplication of previous and next
- Replace every array element by sum of previous and next
- Replace every array element by Bitwise Xor of previous and next element
- Replace every element of the array by its next element
- Replace every element in a circular array by sum of next K elements
- Count pair of strings whose concatenation has every vowel
- Replace all consonants with nearest vowels in a string
- Find the number of words of X vowels and Y consonants that can be formed from M vowels and N consonants
- Lexicographically next string
- Lexicographically next greater string using same character set
- Lexicographically smallest K-length substring containing maximum number of vowels
- Replace consonants with next immediate consonants alphabetically in a String
- Lexicographically next permutation in C++
- Alternate vowel and consonant string
- Print all Subsequences of String which Start with Vowel and End with Consonant.
- Python program to check if given string is vowel Palindrome
- Check if the given string is vowel prime
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

