Print reverse string after removing vowels

16 Jun, 2022
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++
Java
Python3
C#
Javascript
// 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 stringvoid 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 functionint main(){    string s = "geeksforgeeks";    int n = s.length();    replaceOriginal(s, n);     return 0;}
// Java Program for removing characters// from reversed string where vowels are// present in original stringclass 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 Program for removing characters# from reversed string where vowels are# present in original string # Function for replacing the stringdef 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 Codeif __name__ == "__main__":    s = "geeksforgeeks"    n = len(s)    replaceOriginal(s, n) # This code is contributed by# sanjeev2552
// C# Program for removing characters// from reversed string where vowels are// present in original stringusing 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
<script> // JavaScript Program for removing characters// from reversed string where vowels are// present in original string// Function for replacing the stringfunction replaceOriginal(s, n){         // initialize a string of length n        var r = new Array(n);         // Traverse through all characters of string        for (var 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') {                document.write(r[i]);            }        }        document.write("");    } // Driver functionvar s = "geeksforgeeks";var n = s.length;replaceOriginal(s, n); // This code is contributed by shivanisinghss2110 </script>
Output:  
segrfseg
Time complexity : O(n) Auxiliary Space : O(n)
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
 
Print reverse string after removing vowels | GeeksforGeeks - YouTubeGeeksforGeeks529K subscribersPrint reverse string after removing vowels | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 4:00•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=jixtSNRrD5c" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
 
princiraj1992
Rajput-Ji
sanjeev2552
surinderdawra388
shivanisinghss2110
a7977370173
Reverse
vowel-consonant
Searching
Strings
Searching
Strings
Reverse
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Search, insert and delete in a sorted array
Given a sorted and rotated array, find if there is a pair with a given sum
Allocate minimum number of pages
Find common elements in three sorted arrays
Find whether an array is subset of another array
Write a program to reverse an array or string
Reverse a string in Java
Write a program to print all permutations of a given string
C++ Data Types
Check for Balanced Brackets in an expression (well-formedness) using Stack