You are here

C program to check whether a character is vowel or consonant

C program to check whether a character is a vowel or consonant: In our program user will input a character, and we will check whether it is an alphabet or not. If it is an alphabet, then we check whether it is a vowel or a consonant. This code checks whether an input alphabet is a vowel or not. Both lower-case and upper-case are checked.

Check vowel using if else

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.   char ch;
  6.  
  7.   printf("Enter a character\n");
  8.   scanf("%c", &ch);
  9.  
  10.   // Checking both lower and upper case
  11.  
  12.   if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' || ch == 'u' || ch == 'U')
  13.     printf("%c is a vowel.\n", ch);
  14.   else
  15.     printf("%c isn't a vowel.\n", ch);
  16.      
  17.   return 0;
  18. }

Output of program:
C program to check vowel output

Check vowel using switch statement

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.   char ch;
  6.  
  7.   printf("Input a character\n");
  8.   scanf("%c", &ch);
  9.  
  10.   switch(ch)
  11.   {
  12.     case 'a':
  13.     case 'A':
  14.     case 'e':
  15.     case 'E':
  16.     case 'i':
  17.     case 'I':
  18.     case 'o':
  19.     case 'O':
  20.     case 'u':
  21.     case 'U':
  22.       printf("%c is a vowel.\n", ch);
  23.       break;
  24.     default:
  25.       printf("%c isn't a vowel.\n", ch);
  26.   }              
  27.  
  28.   return 0;
  29. }

C program to check vowel or consonant using if else

In this program we will check whether a character is a vowel or consonant or a special character

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.   char ch;
  6.  
  7.   printf("Input a character\n");
  8.   scanf("%c", &ch);
  9.  
  10.   if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' &&ch <= 'Z')) {
  11.     if (ch=='a' || ch=='A' || ch=='e' || ch=='E' || ch=='i' || ch=='I' || ch=='o' || ch=='O' || ch== 'u' || ch=='U')
  12.       printf("%c is a vowel.\n", ch);
  13.     else
  14.       printf("%c is a consonant.\n", ch);
  15.   }
  16.   else
  17.     printf("%c is neither a vowel nor a consonant.\n", ch);
  18.  
  19.   return 0;
  20. }

Function to check vowel

  1. int check_vowel(char a)
  2. {
  3.     if (a >= 'A' && a <= 'Z')
  4.        a = a + 'a' - 'A';   /* Converting to lower case or use a = a + 32 */
  5.  
  6.     if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
  7.        return 1;
  8.  
  9.     return 0;
  10. }

This function can also be used to check if a character is a consonant or not if it's not a vowel, then it will be a consonant, but make sure that the character is an alphabet, not a special character.