Vowel Program In C


We shall learn how to find if a given char is a vowel or consonant.

Using if-else statement

We simply assign a random letter to char type variable and then check it against vowels. As the input can be either lower or upper case letter, so we check all 10 vowels.

#include <stdio.h>

int main()
{
   char ch;
   
   ch = 'n';
   
    if(ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U' )
        printf("%c is a vowel\n", ch);
    else
        printf("%c is a consonent\n", ch);
   
   return 0;
}

Output of the program should be −

n is a consonent

Using switch-case statement

We observe that to check for a char to be a vowel or not, we applied 10 checks in if statement. We can reduce that by using toupper() or tolower() function and narrow down the checks.

#include <stdio.h>

int main()
{
   char ch;

   ch = 'A';

   switch (tolower(ch))
   {
      case 'a':
      case 'e':
      case 'i':
      case 'o':
      case 'u':
         printf("%c is a vowel", ch);
         break;
      default:
         printf("%c is a consonant", ch);
   }

   return 0;
}

Output of the program should be −

A is a vowel

Likewise, we can use toupper() function, and check against upper case characters.

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements