C++ Program to Delete Vowels Characters from String

Here is a C++ program to remove all vowel alphabets from string. In this C++ program, we will delete all vowel characters from given string. The output string must not contain any vowel character.

For Example:
Input String : Orange
Output String : rng
Note : There are five vowels alphabets in english A, E, I, O and U.

Algorithm to delete vowels from string
Let N be a string of length N.
  • Initialize two variables i and j with 0. i and j will act as index pointer for input and output array respectively.
  • Using a loop, traverse string from index 0 to N-1 using variable i.
  • Check if current character is vowel or not. If current element is not vowel then copy it from input array to output array.
  • At the end of the loop, set current element of output array to null character '\0'.

C++ Program to Delete Vowels from String

#include <iostream>
#include <cstring>
using namespace std;
 
int isVowel(char ch);

int main(){
    char input[100], output[100];
    int i, j, writeIndex;
    
    cout << "Enter a string \n";
    cin.getline(input, 500);
    
    for(i = 0, j = 0; input[i] != '\0'; i++){
        if(!isVowel(input[i])){
            // If current character is not a vowel, 
            // copy it to output String
            output[j++] = input[i];
        }
    }
    output[j] = '\0';
     
    cout << "Input String: " << input << endl;
    cout << "String without Vowels: " << output;
     
    return 0;
}
 
/*
 * Function to check whether a character is Vowel or not
 * Returns 1 if character is vowel Otherwise Returns 0 
 */
int isVowel(char ch){
    switch(ch) {
     case 'a':
     case 'e':
     case 'i':
     case 'o':
     case 'u':
     case 'A':
     case 'E':
     case 'I':
     case 'O':
     case 'U': {
        return 1;
    break;
   }
        default :
    return 0;
    }
}
Output
Enter a string 
fsehauk
Input String: fsehauk
String without Vowels: fshuk

In above program, we take a string as input from user and store it in string input. We also defined an output string of same length as input string. Using a for loop traverse input string and and check whether current character is vowel or not by calling isVowel function. If current character is vowel than skip it otherwise copy it from input string to output string. Finally, we print input and output string on screen using cout.


Recommended Posts
C++ Program to Delete Spaces from String or Sentence
C++ Program to Delete a Word from Sentence
C++ Program to Count Words in Sentence
C++ Program to Convert Uppercase to Lowercase Characters
C++ Program to Find Product of Two Numbers
C++ Program to Count Number of Vowels, Consonant, and Spaces in String
C++ Program to Check whether a string is Palindrome or Not
C++ Program to Access Elements of an Array Using Pointer
C++ Program to Print Floyd Triangle
All C++ Programs