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.
Share your thoughts in the comments
Please Login to comment...