Program to delete vowels from a given string
Written by
Delete vowels
The English alphabets {a,e,i,o,u} are called as vowels. Here, we are going to delete all the vowels found in the string given by the user.
Logic:
We will traverse each character, if it is a consonant, then we will add it to a new string, else ignore it. Then we’ll copy the new string to the original string.
Algorithm:
- Input a string.
- Run a for loop to traverse the given string.
- Check each character is a vowel or not, using the function.
- Copy the contents of the new string in the old string.
- Print the modified string
Code:
#include <iostream>
#include <cstring>
using namespace std;
int vowel(char c)
{
if (c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c =='o' || c=='O' || c == 'u' || c == 'U')
return 1; // a vowel
else
return 0; // not a vowel
}
int main()
{
string str,newstr;
cout<<"Enter a string: ";
getline(cin,str);
int len=str.length();
int j=0;
for(int i = 0; i<len; i++)
{
if(vowel(str[i]) == 0)
{
newstr[j] = str[i]; //newstr is string without vowels
j++;
}
}
newstr[j] = '\0'; //terminate the string
strcpy(str, newstr); //copying the new string,
cout<<"Modified String:"<<str;
return 0;
}
Output:
Enter a string: Vowels will be deleted
Modified string: Vwls wll b dltd