Tuesday 24 June 2014

C++ program to delete all vowels from a string

Here is a C++ program to delete all vowels from a string. Here I have given 4 methods to delete vowels. First method simply replaces vowels with space, second method replaces vowels with the next character in string and third and fourth method shows how we can delete vowels using function or recursion.



Method 1 : C++ program to delete all vowels from a string
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    char str[100],temp,vowel[10]={'A','E','I','O','U','a','e','i','o','u'};
    int lenght;
    cout<<"Enter string : ";
    cin>>str;
    for(int i=0;str[i]!='\0';i++)
    {
        for(int y=0;y<9;y++)
            if(str[i]==vowel[y])
                str[i]=' ';
    }
    cout<<"After deleting vowels : "<<str;
    return 0;
}



Method 2 : C++ program to delete all vowels from a string
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    char str[100],vowel[10]={'A','E','I','O','U','a','e','i','o','u'};
    cout<<"Enter string : ";
    cin>>str;
    for(int i=0;str[i]!='\0';i++)
    {
        for(int y=0;y<9;y++)
            if(str[i]==vowel[y])
            {
                for(int k=i;str[k]!='\0';k++)
                      str[k]=str[k+1];
                i--;
            }
    }
    cout<<"After deleting vowels : "<<str;
    return 0;
}




Method 3 : C++ program to delete all vowels from a string using function
#include<iostream>
#include<string.h>
using namespace std;
void dvowel(char (&a)[100])
{
    for(int i=0;a[i]!='\0';i++)
    {
       if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u'
            ||a[i]=='A'||a[i]=='E'||a[i]=='I'||a[i]=='O'||a[i]=='U')
       {
           for(int k=i;a[k]!='\0';k++)
                   a[k]=a[k+1];
           i--;
       }
    }
}
int main()
{
    char str[100];
    cout<<"Enter string : ";
    cin>>str;
    dvowel(str);
    cout<<"After deleting vowels : "<<str;
    return 0;
}



Method 4 : C++ program to delete all vowels from a string using recursion
#include<iostream>
#include<string.h>
using namespace std;
void dvowel(char (&a)[100],int i)
{
    if(a[i]==0)
        return;
    if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u'
        ||a[i]=='A'||a[i]=='E'||a[i]=='I'||a[i]=='O'||a[i]=='U')
            a[i]=' ';
    dvowel(a,i+1);
}
int main()
{
    char str[100];
    cout<<"Enter string : ";
    cin>>str;
    dvowel(str,0);
    cout<<"After deleting vowels : "<<str;
    return 0;
}




No comments:

Post a Comment