Tuesday 17 June 2014

C++ program to check whether a character is vowel or consonant

Here are programs to check whether a character is vowel or consonant. I given 6 different ways you can check whether a character is vowel or consonant like using switch-case, if-else ladder, function, array etc.  


Method 1 : C++ program to check whether a character is vowel or consonant using array
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    char charat,vowels[]={'a','e','i','o','u','A','E','I','O','U'};
    cout<<"Enter the character ";
    charat=getche();
    if(charat<'A'||(charat>'Z'&&charat<'a')||charat>'z')
    {
        cout<<"Character is neither consonant nor vowel";
        return 0;
    }
    for(int i=0;i<=9;i++)
    {
         if(charat==vowels[i])
         {
            cout<<endl<<"Given input is vowel";
            return 0;
         }
     }
     cout<<"given input is consonants ";
     return 0;
}



Method 2 : C++ program to check whether a character is vowel or consonant using if-else ladder
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{

    char str;
    int flag=0;
    cout<<"Enter the Character ";
    str=getche();
    if(str<'A'||(str>'Z'&&str<'a')||str>'z')
    {
        cout<<"Character is neither consonant nor vowel";
        return 0;
    }
    if(str=='a'||str=='A')
        flag=1;
    else if(str=='e'||str=='E')
        flag=1;
    else if(str=='i'||str=='I')
        flag=1;
    else if(str=='o'||str=='O')
        flag=1;
    else if(str=='u'||str=='U')
        flag=1;
    if(flag==1)
       cout<<"\nCharacter is vowel ";
    else
        cout<<"\nCharacter is consonant";
    return 0;
}





Method 3 : C++ program to check whether a character is vowel or consonant using switch cases
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{

    char str;
    int flag=0;
    cout<<"Enter the Character ";
    str=getche();
    if(str<'A'||(str>'Z'&&str<'a')||str>'z')
    {
        cout<<"\nCharacter is neither consonant nor vowel";
        return 0;
    }
    switch(str)
    {
    case 'a':
    case 'A':
    case 'e':
    case 'E':
    case 'i':
    case 'I':
    case 'o':
    case 'O':
    case 'u':
    case 'U':
        cout<<"\nCharacter is vowel";
        break;
    default :
        cout<<"\nCharacter is consonant";
    }
    return 0;
}




Method 4 : C++ program to check whether a character is vowel or consonant using function
#include<iostream>
#include<conio.h>
using namespace std;
int check(char a)
{
    if(a<'A'||(a>'Z'&&a<'a')||a>'z')
       return -1;
    else if(a=='a'||a=='e'||a=='i'||a=='o'||a=='u'
            ||a=='A'||a=='E'||a=='I'||a=='O'||a=='U')
       return 0;
    else
        return 1;
}
int main()
{

    char str;
    int res;
    cout<<"Enter the character ";
    str=getche();
    res=check(str);
    if(res==-1)
        cout<<"\nCharacter is neither consonant nor vowel";
    else if(res==0)
        cout<<"\nCharacter is vowel";
    else
        cout<<"\nCharacter is consonant ";
    return 0;
}



Method 5 : C++ program to check whether a character is vowel or consonant
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{

    char str;
    cout<<"Enter the string ";
    str=getche();
    if(str<'A'||(str>'Z'&&str<'a')||str>'z')
    {
        cout<<"\nCharacter is neither consonant nor vowel";
        return 0;
    }
    if(str<'a')
        str+=32;
    if(str=='a'||str=='e'||str=='i'||str=='o'||str=='u')
        cout<<"\nCharacter is vowel ";
    else
        cout<<"\nCharacter is consonant ";
    return 0;
}



No comments:

Post a Comment