Pages

When Programming is Life

Monday, 9 February 2015

C Programming Vowel Checker

C Programming Vowel Checker
C Programming Vowel Checker


Hello guys, today I am gonna show you hot to check if the character input by user is either vowel or consonant. I know it's a very simple and probably most easy problem but still this can be little hard for others who are completely new to C Programming. 

Here I have shown Three methods by which you can check the input character. First one is by using if condition which is most easy, second one is using switch statement which is also easy and the last one is using our own function which is considerably more complicated. So lets see the three codes.

Using If Statement

#include<stdio.h>
#include<conio.h 
int main()
{
  char ch;
 
  printf("Enter a character\n");
  scanf("%c", &ch);
 
  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 not a vowel.\n", ch);
  getch();
  return 0;
}

Using Switch Statement

#include <stdio.h>
#include<conio.h>
 
int main()
{
  char ch;
 
  printf("Input a character\n");
  scanf("%c", &ch);
 
  switch(ch)
  {
    case 'a':
    case 'A':
    case 'e':
    case 'E':
    case 'i':
    case 'I':
    case 'o':
    case 'O':
    case 'u':
    case 'U':
      printf("%c is a vowel.\n", ch);
      break;
    default:
      printf("%c is not a vowel.\n", ch);
  }              
  getch();
  return 0;
}

Our Own Vowel Checker Function

int check_vowel(char a)
{
    if (a >= 'A' && a <= 'Z')
       a = a + 'a' - 'A';   /* Converting to lower case or use a = a + 32 */
 
    if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
       return 1;
 
    return 0;
}

Word of Caution: You can also make similar Function to check whether the character is consonant or not. But keep in mind that you always do the range checking otherwise your program will not be fool proof against special character as input.
Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

1 comments

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

 
Posts RSSComments RSSBack to top
© 2011 Standard Programming ∙ Designed by BlogThietKe
Released under Creative Commons 3.0 CC BY-NC 3.0