C++ Program to Remove Vowels from a String

Program to Remove the Vowels from a string

In this article we will learn how to write Program to Remove the Vowels from a string. To remove vowels first we will take a string as an input from the user then we will check for the vowels by iterating each character of the string through the for loop and if the character iterated is found to be vowel then we will remove that from the string. And then we print the remaining string, without vowels as an output.

Program to Remove the Vowels from a string

Algorithm:

  • Initialize the variables.
  • Accept the input.
  • Initialize for loop.
  • Check and remove the vowels from the string.
  • Store the string without vowels using another for loop.
  • Terminate both for loop.
  • Print the string without vowels.

Method 1

Run
// Write a c++ program to remove vowels from a given string.
#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
	// initializing variable
	char str[100];

	// accepting input
	cout << "Enter a string : "; cin >> str;
	
	int len = strlen(str);

	// iterating the string
	for(int i=0; i<len; i++)
	{   
	    // checking vowels.
		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')
		{

		    // deleting vowels by shifting left all upcoming characters left 
			for(int j=i; j<len; j++)
			{
				str[j]=str[j+1];
			}
		i--;
		len--;
		}
	}
	// can directly print str
	// as '\0' also shifted left as many times as vowels were found
	cout << "After removing Vowels: " << str;

    return 0;	
}

Output :

Enter a string : aaabbbeeeccc
After removing Vowels: bbbccc

Method 2

Objective: Write a program that will take one string as input. the program will then remove vowels a e i o u

Run
#include <bits/stdc++.h>
using namespace std;

string remVowel(string str)
{
  regex r("[aeiouAEIOU]");
  return regex_replace(str, r, "");
}

// Driver Code
int main()
{
  string str = "Prepinsta"; 
  cout << "String after removing vowels "<<(remVowel(str));
  return 0;
}

Output

String after removing vowels Prpnst

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

  • Length of the string without using strlen() function : C | C++ | Java | Python
  • Toggle each character in a string : C | C++ | Java | Python
  • Count the number of vowels : C | C++ | Java | Python
  • Remove the vowels from a String : C | C++ | Java | Python
  • Check if the given string is Palindrome or not : C | C++ | Java | Python
  • Print the given string in reverse order : C | C++ | Java | Python
  • Remove all characters from string except alphabets : C | C++ | Java | Python
  • Remove spaces from a string : C | C++ | Java | Python
  • Remove brackets from an algebraic expression : C | C++ | Java | Python
  • Count the sum of numbers in a string : C | C++ | Java | Python
  • Capitalize the first and last character of each word of a string : C | C++ | Java | Python
  • Calculate frequency of characters in a string : C | C++ | Java | Python
  • Find non-repeating characters in a string : C | C++ | JAVA | Python
  • Check if two strings are Anagram or not : C | C++ | Java | Python
  • Replace a sub-string in a string: C | C++ | Java | Python
  • Count common sub-sequence in two strings : C | C++ | Java | Python
  • Check if two strings match where one string contains wildcard characters : C | C++ | Java | Python

One comment on “C++ Program to Remove Vowels from a String”


  • Deep

    #include
    using namespace std;

    int main()
    {
    string str = “aaabbbeeeccc”;
    int len = str.length();

    for (int i = 0; i<len; i++)
    {
    while (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')
    {
    i++;
    }

    cout << str[i];

    }

    return 0;
    }