Print reverse string after removing vowels


The reverse() is a pre-installed and predefined header file, which is used to define as a template in a process in a C++ environment. The method is able to reverse the elements from last to first manner in a range for any value container. For this process the time complexity is O(n). Lets assume, we have a string or a sentance declared as str[] with some data elements and now the task is to perform reverse process after removing the vowels from that string to get the final outcome.

Here is some general examples of the process method −

Input taken for the process:duck
Output by the process:kud
Input taken for the process: hello
Output by the process is here: holle
Input taken for the process: hello world
Output by the process: hollo werld

Algorithm to reverse a string after removing the vowels from it

In this possible algorithm, we are going to perform the printing process of a sentence without vowels and reverse it in a C++ environment. With this algorithm, we will build some C++ syntax to learn about the problem statement in an efficient manner.

  • Step 1 − Start the process.

  • Step 2 − Declare the input output stream.

  • Step 3 − Import the built-in classes and declared functions.

  • Step 4 − Declare the values of the string.

  • Step 5 − Construct a return set of the vowels.

  • Step 6 − Print the resultant string.

  • Step 7 − Construct a notation for the removing vowels.

  • Step 8 − Print the first character.

  • Step 9 − Mark a loop to check the each character value.

  • Step 10 − Compare the value of the consecutive characters.

  • Step 11 − Declare a driver string.

  • Step 12 − Remove the vowels and get return value.

  • Step 13 − Run the reversal method on the string without vowels.

  • Step 13 − Get the result and terminate the process.

Syntax to reverse a string after removing the vowels from it

vector<char> vowels = {'a', 'e', 'i', 'o', 'u','A', 'E', 'I', 'O', 'U';
for (int i = 0; i < str.length(); i++){
      if (find(vowels.begin(), vowels.end(),
      str[i]) != vowels.end()){
         str = str.replace(i, 1, "");
         i -= 1;
      }
   }
   return str;
}
int main(){
string str = "STRING STATYEMENT ONE" " STRING STATEMENT 2";
cout << remVowel(str) << endl;
string remVowel(string str){
   regex r("[aeiouAEIOU]");
   return regex_replace(str, r, "");
}
int main(){
string str = "STRING STATEMENT";
cout << (remVowel(str));

In this possible syntax, we will perform the printing process of a sentence or a string without vowels present in that particular content and reverse the whole result string in a C++ environment. With this algorithm, we will build some C++ programs to get a broad view about the problem statement.

Approaches to Follow

  • Approach 1 − C++ program to reverse a string after removing vowels by using iteration, traversal and min_cost (String str) method

  • Approach 2 − C++ program to reverse a string after removing vowels by using utility functions, two individual pointers and findwinner (String str) method

Approach 1: Using Iteration, Traversal and Min_cost (String str) Method

Use of Iteration Method

In this possible approach, we are going to apply the iteration method to print a string or sentence without vowels. After removing the vowels we will reverse the whole particular string again to get the desired result.

char str[100];
int i, j, len = 0;
printf("ENTER THE VALUE OF THE STRING : ");
scanf("%s", str);
len = strlen(str);
for (i = 0; i < len; i++) {
   if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' ||
   str[i] == 'u' ||
   str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' ||
   str[i] == 'U') {
      for (j = i; j < len; j++) {
         str[j] = str[j + 1];
      }
      i--;
      len--;
   }
   str[len + 1] = '\0';
}
printf("AFTER REMOVING VOWEL STATEMENT HERE : %s", str);

Example

//C++ program to reverse a string after removing vowels by using iteration
#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 = "ARBRDDKOLKATADHAKAINDIABANGLADESH";
   string reversed = reverseString(s);
   string withoutVowels = removeVowels(s,reversed);
   cout << withoutVowels << endl;
   return 0;
}

Output

SEDALGABINIKDAKOKDRRA

Use of the Traversal Method

In this possible approach, we are going to apply the traversal method to print a string or sentence without vowels. After removing the vowels we will reverse the whole particular string again to get the desired result.

Example

//C++ program to reverse a string after removing vowels by using traversal method
#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 = "KOLKATADHAKAARBRDDINDBANGLADESH";
   int n = s.length();
   replaceOriginal(s, n);
   return 0;
}

Output

HSEDALGNABDNIDDRBRAAKAHDATAKLOK

Use of Min_cost(string st) Method

In this possible approach, we have applied the min_cost(string st) method to print a string or sentence without vowels. After removing the vowels we will reverse the whole particular string again to get the desired result.

Example

//C++ program to reverse a string after removing vowels and capture the cost of that reverse string by using min_cost(string st)
#include<bits/stdc++.h>
using namespace std;
int min_cost(string st){
   string vow = "aeiou";
   int cost = 0;
   for(int i = 0; i < st.size(); i++){
      vector<int> costs;
      for(int j = 0; j < 5; j++)
      costs.push_back(abs(st[i] - vow[j]));
      cost += *min_element(costs.begin(),
      costs.end());
   }
   return cost;
}
int main(){
   string str = "arbrddindbangladeshkolkatadhaka";
   cout << (min_cost(str));
}

Output

34

Approach 2: Using Utility Functions, two Individual Pointers and Findwinner (String str) Method

Use of the Utility Functions

In this possible approach, we have applied the various utility functions method to print a string or sentence without vowels. After removing the vowels we will reverse the whole particular string again to get the desired result.

char s[100], t[100];
int c, d = 0;
printf("VOWEL DELETE STATEMENT IS HERE\n");
gets(s);
for (c = 0; s[c] != '\0'; c++){
   if (check_vowel(s[c]) == 0){
      t[d] = s[c];
      d++;
   }
   for (int i = s.length() - 1; i >= 0; i--){
      reversed += s[i];
      return withoutVowels;
   }
   t[d] = '\0';
   strcpy(s, t);
   printf("String after deleting vowels: %s\n", s);
   return 0;
}
int check_vowel(char t){
if (t == 'a' || t == 'A' || t == 'e' || t == 'E' || t == 'i' || t == 'I' || t
=='o' || t=='O' || t == 'u' || t == 'U')
return 1;

Example

//C++ program to reverse a string after removing vowels by using utility function to check for vowel
#include<bits/stdc++.h>
using namespace std;
bool isVowel(char c){
   return (c=='a' || c=='A' || c=='e' ||
   c=='E' || c=='i' || c=='I' ||
   c=='o' || c=='O' || c=='u' ||
   c=='U');
}
string reverseVowel(string str){
   int j=0;
   string vowel;
   for (int i=0; str[i]!='\0'; i++)
   if (isVowel(str[i]))
   vowel[j++] = str[i];
   for (int i=0; str[i]!='\0'; i++)
   if (isVowel(str[i]))
   str[i] = vowel[--j] ;
   return str;
}
int main(){
   string str = "GOOD MORNING KOLKATA! I AM GOING TO DHAKA!";
   cout << reverseVowel(str);
   return 0;
}

Output

GAAD MORNING KOLKATI! A AM GOING TO DHOKO!

Use of the two Pointers Method

In this possible approach, we are going to declare and apply the two pointers on a string to print that string or sentence without vowels. After removing the vowels we will reverse the whole particular string again to get the desired result.

Example

//C++ program to reverse a string after removing vowels by using two pointers to check for vowel
#include<bits/stdc++.h>
using namespace std;
bool isVowel(char c){
   return (c=='a' || c=='A' || c=='e' ||
   c=='E' || c=='i' || c=='I' ||
   c=='o' || c=='O' || c=='u' ||
   c=='U');
}
string reverseVowel(string str){
   int i = 0;
   int j = str.length()-1;
   while (i < j){
      if (!isVowel(str[i])){
         i++;
         continue;
      }
      if (!isVowel(str[j])){
         j--;
        continue;
      }
      swap(str[i], str[j]);
      i++;
      j--;
   }
   return str;
}
int main(){
   string str = "I AM HERE IN DHAKA! IT IS A BEAUTIFUL CITY!";
   cout << reverseVowel(str);
   return 0;
}

Output

I UM HIRU AN DHEKA! IT IS A BAIETEFAL CITY!

Use of the FindWinner(string s) Method

In this possible approach, we are going to apply the findWinner(String S) to print a string or sentence without vowels. After removing the vowels we will reverse the whole particular string again to get the desired result. It is an an application level approach to find winner of a game by demonstrating the string reverse process and find the winner of the game by using the findWinner(string s ).

Example

//C++ program to create an application level example to find winner game by demonstrating the string reverse process and find the winner of the game by using the findWinner(string s)
#include <bits/stdc++.h>
using namespace std;
void findWinner(string s){
   int vowels_count = 0,
   consonants_count = 0;
   for (int i = 0; i < s.size(); i++){
      if (s[i] == 'a'
      || s[i] == 'e'
      || s[i] == 'i'
      || s[i] == 'o'
      || s[i] == 'u'){
         vowels_count++;
      } else {
         consonants_count++;
      }
   }
   if (vowels_count == 0){
      if (consonants_count % 2 == 0){
         cout << "Player B";
      } else{
         cout << "Player A";
      }
   } else if (vowels_count == 1
   && consonants_count % 2 != 0){
      cout << "Player A";
   } else {
      cout << "D";
   }
}
int main(){
   string s = "Hi Rebeca, Rudra Here! How Are You?";
   findWinner(s);
   return 0;
}

Output

D

Conclusion

Today in this article we have learned about how to implement the process to construct and apply the various method to print a sentence without vowels from a sentance or a string and, reverse the result value in a C++ environment. With the above mentioned logics, syntax and algoritm; we have tried to build some C++ codes to solve the problem statement in an efficient manner.

Updated on: 27-Dec-2023

39 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements