Print reverse string after removing vowels
Last Updated :
06 Dec, 2023
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.
C++
#include <iostream>
#include <string>
using namespace std;
string reverseString(string s) {
string reversed;
for ( int i = s.length() - 1; i >= 0; i--) {
reversed += s[i];
}
return reversed;
}
string removeVowels(string s,string reversed) {
string withoutVowels;
for ( int i = 0; i < s.length(); i++) {
char c = s[i];
if (c != 'a' && c != 'e' && c != 'i' && c != 'o' && c != 'u' && c != 'A' && c != 'E' && c != 'I' && c != 'O' && c != 'U' ) {
withoutVowels += reversed[i];
}
}
return withoutVowels;
}
int main() {
string s = "geeksforgeeks" ;
string reversed = reverseString(s);
string withoutVowels = removeVowels(s,reversed);
cout << withoutVowels << endl;
return 0;
}
|
Java
public class Main {
public static void main(String[] args) {
String s = "geeksforgeeks" ;
String reversed = reverseString(s);
String withoutVowels = removeVowels(s, reversed);
System.out.println(withoutVowels);
}
public static String reverseString(String s) {
String reversed = "" ;
for ( int i = s.length() - 1 ; i >= 0 ; i--) {
reversed += s.charAt(i);
}
return reversed;
}
public static String removeVowels(String s, String reversed) {
String withoutVowels = "" ;
for ( int i = 0 ; i < s.length(); i++) {
char c = s.charAt(i);
if (c != 'a' && c != 'e' && c != 'i' && c != 'o' && c != 'u' && c != 'A' && c != 'E' && c != 'I' && c != 'O' && c != 'U' ) {
withoutVowels += reversed.charAt(i);
}
}
return withoutVowels;
}
}
|
Python3
def reverseString(s):
reversed_string = ""
for i in range ( len (s) - 1 , - 1 , - 1 ):
reversed_string + = s[i]
return reversed_string
def removeVowels(s, reversed_string):
withoutVowels = ""
for i in range ( len (s)):
c = s[i]
if c not in [ 'a' , 'e' , 'i' , 'o' , 'u' , 'A' , 'E' , 'I' , 'O' , 'U' ]:
withoutVowels + = reversed_string[i]
return withoutVowels
if __name__ = = "__main__" :
s = "geeksforgeeks"
reversed_string = reverseString(s)
withoutVowels = removeVowels(s, reversed_string)
print (withoutVowels)
|
C#
using System;
namespace ConsoleApp1 {
class Program {
static string ReverseString( string s)
{
string reversed = "" ;
for ( int i = s.Length - 1; i >= 0; i--) {
reversed += s[i];
}
return reversed;
}
static string RemoveVowels( string s, string reversed)
{
string withoutVowels = "" ;
for ( int i = 0; i < s.Length; i++) {
char c = s[i];
if (c != 'a' && c != 'e' && c != 'i' && c != 'o'
&& c != 'u' && c != 'A' && c != 'E'
&& c != 'I' && c != 'O' && c != 'U' ) {
withoutVowels += reversed[i];
}
}
return withoutVowels;
}
static void Main( string [] args)
{
string s = "geeksforgeeks" ;
string reversed = ReverseString(s);
string withoutVowels = RemoveVowels(s, reversed);
Console.WriteLine(withoutVowels);
}
}
}
|
Javascript
function reverseString(s) {
let reversed = "" ;
for (let i = s.length - 1; i >= 0; i--) {
reversed += s[i];
}
return reversed;
}
function removeVowels(s, reversed) {
let withoutVowels = "" ;
for (let i = 0; i < s.length; i++) {
let c = s[i];
if (c != 'a' && c != 'e' && c != 'i' && c != 'o' && c != 'u' && c != 'A' && c != 'E' && c != 'I' && c != 'O' && c != 'U' ) {
withoutVowels += reversed[i];
}
}
return withoutVowels;
}
let s = "geeksforgeeks" ;
let reversed = reverseString(s);
let withoutVowels = removeVowels(s, reversed);
console.log(withoutVowels);
|
Time complexity : O(n)
Auxiliary Space : O(n)
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++
#include <bits/stdc++.h>
using namespace std;
void replaceOriginal(string s, int n)
{
string r(n, ' ' );
for ( int i = 0; i < n; i++) {
r[i] = s[n - 1 - i];
if (s[i] != 'a' && s[i] != 'e' && s[i] != 'i'
&& s[i] != 'o' && s[i] != 'u' ) {
cout << r[i];
}
}
cout << endl;
}
int main()
{
string s = "geeksforgeeks" ;
int n = s.length();
replaceOriginal(s, n);
return 0;
}
|
Java
class GFG {
static void replaceOriginal(String s, int n) {
char r[] = new char [n];
for ( int i = 0 ; i < n; i++) {
r[i] = s.charAt(n - 1 - 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( "" );
}
public static void main(String[] args) {
String s = "geeksforgeeks" ;
int n = s.length();
replaceOriginal(s, n);
}
}
|
Python3
def replaceOriginal(s, n):
r = [ ' ' ] * n
for i in range (n):
r[i] = s[n - 1 - 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 ()
if __name__ = = "__main__" :
s = "geeksforgeeks"
n = len (s)
replaceOriginal(s, n)
|
C#
using System;
class GFG
{
static void replaceOriginal(String s, int n)
{
char []r = new char [n];
for ( int i = 0; i < n; i++)
{
r[i] = s[n - 1 - i];
if (s[i] != 'a' && s[i] != 'e' && s[i] != 'i'
&& s[i] != 'o' && s[i] != 'u' )
{
Console.Write(r[i]);
}
}
Console.WriteLine( "" );
}
public static void Main(String[] args)
{
String s = "geeksforgeeks" ;
int n = s.Length;
replaceOriginal(s, n);
}
}
|
Javascript
<script>
function replaceOriginal(s, n)
{
var r = new Array(n);
for ( var i = 0; i < n; i++) {
r[i] = s.charAt(n - 1 - 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( "" );
}
var s = "geeksforgeeks" ;
var n = s.length;
replaceOriginal(s, n);
</script>
|
Complexity Analysis:
- Time complexity : O(n)
- Auxiliary Space : O(n)
Approach:
In this approach, we will iterate through each character of the string, and if the character is not a vowel, we will append it to a new string. Finally, we will reverse the new string and return it as the output.
Steps:
- Create an empty string called “new_string”.
- Iterate through each character of the input string.
- If the character is not a vowel, append it to “new_string”.
- Reverse the “new_string” using string slicing.
- Return the reversed string as the output.
C++
#include <iostream>
#include <string>
#include<bits/stdc++.h>
std::string reverseStringWithoutVowels( const std::string& inputString) {
std::string vowels = "AEIOUaeiou" ;
std::string newString = "" ;
for ( char ch : inputString) {
if (vowels.find(ch) == std::string::npos) {
newString += ch;
}
}
std::reverse(newString.begin(), newString.end());
return newString;
}
int main() {
std::string inputString = "geeksforgeeks" ;
std::string outputString = reverseStringWithoutVowels(inputString);
std::cout << outputString << std::endl;
return 0;
}
|
Java
import java.util.Scanner;
public class ReverseStringWithoutVowels {
static String reverseStringWithoutVowels(String inputString) {
String vowels = "AEIOUaeiou" ;
StringBuilder newString = new StringBuilder();
for ( char ch : inputString.toCharArray()) {
if (vowels.indexOf(ch) == - 1 ) {
newString.append(ch);
}
}
newString.reverse();
return newString.toString();
}
public static void main(String[] args) {
String inputString = "geeksforgeeks" ;
String outputString = reverseStringWithoutVowels(inputString);
System.out.println(outputString);
}
}
|
Python3
def reverse_string_without_vowels(input_string):
vowels = "AEIOUaeiou"
new_string = ""
for char in input_string:
if char not in vowels:
new_string + = char
return new_string[:: - 1 ]
input_string = "geeksforgeeks"
output_string = reverse_string_without_vowels(input_string)
print (output_string)
|
C#
using System;
class Program
{
static string ReverseStringWithoutVowels( string inputString)
{
string vowels = "AEIOUaeiou" ;
string newString = "" ;
foreach ( char c in inputString)
{
if (vowels.IndexOf(c) == -1)
{
newString += c;
}
}
char [] charArray = newString.ToCharArray();
Array.Reverse(charArray);
return new string (charArray);
}
static void Main()
{
string inputString = "geeksforgeeks" ;
string outputString = ReverseStringWithoutVowels(inputString);
Console.WriteLine(outputString);
}
}
|
Javascript
function reverseStringWithoutVowels(inputString) {
const vowels = "AEIOUaeiou" ;
let newString = "" ;
for (let i = inputString.length - 1; i >= 0; i--) {
if (!vowels.includes(inputString[i])) {
newString += inputString[i];
}
}
return newString;
}
const inputString = "geeksforgeeks" ;
const outputString = reverseStringWithoutVowels(inputString);
console.log(outputString);
|
Time Complexity: The time complexity of this approach is O(n), where n is the length of the input string.
Auxiliary Space: The auxiliary space used in this approach is O(n), where n is the length of the input string.
Similar Reads
Reverse vowels in a given string
Given a string s, reverse only the vowels in s while keeping the other characters in their original positions. Examples: Input: "geeksforgeeks"Output: "geeksforgeeks"Explanation: The vowels 'e', 'e', 'o', 'e', 'e' are reversed, resulting in "geeksforgeeks". Input: "helloworld"Output: "hollowerld"Exp
9 min read
Print words of a string in reverse order
Let there be a string say "I AM A GEEK". So, the output should be "GEEK A AM I" . This can done in many ways. One of the solutions is given in Reverse words in a string . Examples: Input : I AM A GEEK Output : GEEK A AM I Input : GfG IS THE BEST Output : BEST THE IS GfG This can be done in more simp
10 min read
Remove vowels from a string stored in a Binary Tree
Given a binary tree in such a way such that the level order traversal of a binary tree produces a string S. The task is to remove all vowels from the binary tree and print the level order traversal of the remaining tree.Examples: Input: G / \ E E / \ K S Output: G / \ K S Input: G / \ O A / L Output
11 min read
Remove consecutive vowels from string
Given a string s of lowercase letters, we need to remove consecutive vowels from the string Note : Sentence should not contain two consecutive vowels ( a, e, i, o, u). Examples : Input: geeks for geeksOutput: geks for geksInput : your article is in queue Output : yor article is in quApproach: Iterat
15+ min read
Program to duplicate Vowels in String
Given a string "str", the task is to duplicate vowels in this string. Examples: Input: str = "geeks"Output: geeeeks Input: str = "java"Output: jaavaa Approach: Iterate the string using a loop. Check if the character is a vowel and duplicate it. Return then print the resultant string. Below is the im
5 min read
Reverse a String | Shell Programming
In shell scripting, reversing a string can be done using various methods, such as rev, awk, sed, and Perl. Here, we provide examples of reversing a string using different approaches and commands in Unix/Linux systems. We are given a string and we have to use shell script to print it in the reverse o
5 min read
Longest substring having K distinct vowels
Given a string s we have to find the length of the longest substring of s which contain exactly K distinct vowels. Note: Consider uppercase and lowercase characters as two different characters. Examples: Input : s = "tHeracEBetwEEntheTwo", k = 1 Output : 14 Explanation : Longest substring with only
13 min read
Alternate vowel and consonant string
Given a string, rearrange the characters of the given string such that the vowels and consonants occupy the alternate position. If the string can not be rearranged in the desired way, print "no such string". The order of vowels with respect to each other and the order of consonants with respect to e
15+ min read
Remove spaces from a given string
Given a string, remove all spaces from the string and return it. Input: "g eeks for ge eeks "Output: "geeksforgeeks" Input: "abc d "Output: "abcd" Expected time complexity is O(n) and only one traversal of string. Below is a Simple Solution 1) Iterate through all characters of given string, do follo
8 min read
Program to remove vowels from Linked List
Given a singly linked list, the task is to remove the vowels from the given linked list. Examples: Input: g -> e -> e -> k -> s -> f -> o -> r -> g -> e -> e -> k -> s Output: g -> k -> s -> f -> r -> g -> k -> s Explanation: After removing vo
15 min read