Remove vowels from a String in C++


The following C++ program illustrates how to remove the vowels (a,e, i,u,o) from a given string. In this context, we create a new string and process input string character by character, and if a vowel is found it is excluded in the new string, otherwise the character is added to the new string after the string ends we copy the new string into the original string. The algorithm is as follows;

Algorithm

START
   Step-1: Input the string
   Step-3: Check vowel presence, if found return TRUE
   Step-4: Copy it to another array
   Step-5: Increment the counter
   Step-6: Print
END

In pursuance of the above algorithm, the following code in c++ language essayed as following;

Example

#include <iostream>
#include <string.h>
#include <conio.h>
#include <cstring>
using namespace std;
int vowelChk(char);
int main(){
   char s[50], t[50];
   int c, d = 0;
   cout<<"Enter a string to delete vowels\n";
   cin>>s;
   for(c = 0; s[c] != '\0'; c++) {
      // check for If not a vowel
      if(vowelChk(s[c]) == 0){
         t[d] = s[c];
         d++;
      }
   }
   t[d] = '\0';
   strcpy(s, t);
   cout<<"String after delete vowels:"<<s;
   return 0;
}
int vowelChk(char ch){
   if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' || ch == 'u' || ch == 'U')
      return 1;
   else
      return 0;
}

This C++ program deletes vowels from a string: if the input string is "ajaykumar" then it yields results as "jykmr". Finally, we obtain a string without vowels.

Output

Enter a string to delete vowels
ajaykumar
String after delete vowels:jykmr

Updated on: 29-Nov-2019

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements