Given a string s, print reverse of string and remove the characters from the reversed string where there are vowels in the original string.
Examples:
Input : geeksforgeeks Output : segrfseg Explanation : Reversed string is skeegrofskeeg, removing characters from indexes 1, 2, 6, 9 & 10 (0 based indexing), we get segrfseg . Input :duck Output :kud
A simple solution is to first reverse the string, then traverse the reversed string and remove vowels.
An efficient solution is to do both tasks in one traversal.
Create an empty string r and traverse the original string s and assign the value to the string r. Check whether, at that index, the original string contains a consonant or not. If yes then print the element at that index from string r.
Basic implementation of the above approach :
C++
// CPP Program for removing characters // from reversed string where vowels are // present in original string #include <bits/stdc++.h> using namespace std; // Function for replacing the string void replaceOriginal(string s, int n) { // initialize a string of length n string r(n, ' '); // Traverse through all characters of string for (int i = 0; i < n; i++) { // assign the value to string r // from last index of string s r[i] = s[n - 1 - i]; // if s[i] is a consonant then // print r[i] if (s[i] != 'a' && s[i] != 'e' && s[i] != 'i' && s[i] != 'o' && s[i] != 'u') { cout << r[i]; } } cout << endl; } // Driver function int main() { string s = "geeksforgeeks"; int n = s.length(); replaceOriginal(s, n); return 0; } |
Java
// Java Program for removing characters // from reversed string where vowels are // present in original string class GFG { // Function for replacing the string static void replaceOriginal(String s, int n) { // initialize a string of length n char r[] = new char[n]; // Traverse through all characters of string for (int i = 0; i < n; i++) { // assign the value to string r // from last index of string s r[i] = s.charAt(n - 1 - i); // if s[i] is a consonant then // print r[i] if (s.charAt(i) != 'a' && s.charAt(i) != 'e' && s.charAt(i) != 'i' && s.charAt(i) != 'o' && s.charAt(i) != 'u') { System.out.print(r[i]); } } System.out.println(""); } // Driver function public static void main(String[] args) { String s = "geeksforgeeks"; int n = s.length(); replaceOriginal(s, n); } } // This code is contributed by princiRaj1992 |
Python3
# Python3 Program for removing characters # from reversed string where vowels are # present in original string # Function for replacing the string def replaceOriginal(s, n): # initialize a string of length n r = [' '] * n # Traverse through all characters of string for i in range(n): # assign the value to string r # from last index of string s r[i] = s[n - 1 - i] # if s[i] is a consonant then # print r[i] if (s[i] != 'a' and s[i] != 'e' and s[i] != 'i' and s[i] != 'o' and s[i] != 'u'): print(r[i], end = "") print() # Driver Code if __name__ == "__main__": s = "geeksforgeeks" n = len(s) replaceOriginal(s, n) # This code is conributed by # sanjeev2552 |
C#
// C# Program for removing characters // from reversed string where vowels are // present in original string using System; class GFG { // Function for replacing the string static void replaceOriginal(String s, int n) { // initialize a string of length n char []r = new char[n]; // Traverse through all characters of string for (int i = 0; i < n; i++) { // assign the value to string r // from last index of string s r[i] = s[n - 1 - i]; // if s[i] is a consonant then // print r[i] if (s[i] != 'a' && s[i] != 'e' && s[i] != 'i' && s[i] != 'o' && s[i] != 'u') { Console.Write(r[i]); } } Console.WriteLine(""); } // Driver code public static void Main(String[] args) { String s = "geeksforgeeks"; int n = s.Length; replaceOriginal(s, n); } } // This code is contributed by Rajput-JI |
Output:
segrfseg
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:
- Modify string by removing vowels in between two consonants
- Reverse vowels in a given string
- Print string after removing all (“10” or “01”) from the binary string
- Print reverse of a string using recursion
- Print words of a string in reverse order
- Check if a string can be converted to another string by replacing vowels and consonants
- Print an N x M matrix such that each row and column has all the vowels in it
- Print number of words, vowels and frequency of each character
- Removing string that is an anagram of an earlier string
- First X vowels from a string
- Modify the string such that it contains all vowels at least once
- Permutations of string such that no two vowels are adjacent
- Count the pairs of vowels in the given string
- Remove consecutive vowels from string
- Program to remove vowels from a String
- Longest Subsequence of a String containing only vowels
- Program to duplicate Vowels in String
- Check whether the vowels in a string are in alphabetical order or not
- Replace all consonants with nearest vowels in a string
- Replace every vowels with lexicographically next vowel in a String
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.

