Tuesday, 17 June 2014

C++ program to find number of vowels in given string

Here is a program to find number of vowels in given string in C++. I given 6 different ways you can find number of vowels in given string like using switch-case, if-else ladder, function, recursion etc. I have also given a program to find number of words starting with vowels.


Method 1 : C++ program to find number of vowels in given string using array
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{

    char str[20],vowels[]={'a','e','i','o','u','A','E','I','O','U'};
    int vowel=0;
    cout<<"Enter the string ";
    gets(str);
    for(int i=0;str[i]!='\0';i++)
    {
        for(int y=0;y<10;y++)
            if(str[i]==vowels[y])
                vowel++;
    }
    cout<<"Number of vowels "<<vowel;
    return 0;
}



Method 2 : C++ program to find number of vowels in given string using if-else ladder
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{

    char str[20];
    int vowel=0;
    cout<<"Enter the string ";
    gets(str);
    for(int i=0;str[i]!='\0';i++)
    {
        if(str[i]=='a'||str[i]=='A')
            vowel++;
        else if(str[i]=='e'||str[i]=='E')
            vowel++;
        else if(str[i]=='i'||str[i]=='I')
            vowel++;
        else if(str[i]=='o'||str[i]=='O')
            vowel++;
        else if(str[i]=='u'||str[i]=='U')
            vowel++;
    }
    cout<<"Number of vowels "<<vowel;
    return 0;
}




Method 3 : C++ program to find number of vowels in given string using switch cases
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{

    char str[20];
    int vowel=0;
    cout<<"Enter the string ";
    gets(str);
    for(int i=0;str[i]!='\0';i++)
    {
        switch(str[i])
        {
        case 'a':
        case 'A':
        case 'e':
        case 'E':
        case 'i':
        case 'I':
        case 'o':
        case 'O':
        case 'u':
        case 'U':
            vowel++;
            break;
        }
    }
    cout<<"Number of vowels "<<vowel;
    return 0;
}




Method 4 : C++ program to find number of vowels in given string using function
#include<iostream>
#include<stdio.h>
using namespace std;
int countv(char *str)
{
    char vowels[]={'a','e','i','o','u','A','E','I','O','U'};
    int vowel=0;
    for(int i=0;str[i]!='\0';i++)
    {
        for(int y=0;y<10;y++)
            if(str[i]==vowels[y])
                vowel++;
    }
    return vowel;
}
int main()
{

    char str[20];
    int vowel=0;
    cout<<"Enter the string ";
    gets(str);
    vowel=countv(str);
    cout<<"Number of vowels "<<vowel;
    return 0;
}




Method 5 : C++ program to find number of vowels in given string using recursion
#include<iostream>
#include<stdio.h>
using namespace std;
int countv(char *str,int vowel,int i)
{
    if(str[i]==0)
      return vowel;
    switch(str[i])
    {
    case 'a':
    case 'A':
    case 'e':
    case 'E':
    case 'i':
    case 'I':
    case 'o':
    case 'O':
    case 'u':
    case 'U':
        vowel++;
        break;
    }
    countv(str,vowel,i+1);
}
int main()
{

    char str[20];
    int vowel=0;
    cout<<"Enter the string ";
    gets(str);
    vowel=countv(str,0,0);
    cout<<"Number of vowels "<<vowel;
    return 0;
}



Method 6 : C++ program to find number of vowels in given string
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{

    char str[20];
    int vowel=0;
    cout<<"Enter the string ";
    gets(str);
    for(int i=0;str[i]!='\0';i++)
    {
        if(str[i]<'a')
            str[i]+=32;
        if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u')
           vowel++;
    }
    cout<<"Number of vowels "<<vowel;
    return 0;
}



C++ program to count the number of words starting with vowels
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    char str[100],temp;
    int nvowel=0;
    cout<<"Enter string : ";
    cin.getline(str,100);
    for(int i=0;str[i]!='\0';i++)
    {
        if(i==0||str[i-1]==' ')
        {
            if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u')
               nvowel++;
            else if(str[i]=='A'||str[i]=='E'||str[i]=='I'||str[i]=='O'||str[i]=='U')
               nvowel++;
        }
    }
    cout<<"Number of words starting with vowels : "<<nvowel;
    return 0;
}

OUTPUT

No comments:

Post a Comment