Skip to main content

C Program to check character is vowel or consonant using command line argument

Below code will check whether character passed from command line is vowel or consonant.

“A”, “E”,”I”,”O”,”U” either in upper or lower case character are vowels. Other than “A”, “E”,”I”,”O”,”U” all characters between A to Z are consonant.

//Program Name: Accept a lower or Upper case character from the user from command line 
//for example program name character (main.c a)
//and check whether the character is a vowel or consonant.

#include<stdio.h>
#include<conio.h>
#include<string.h>
 void main(int argc, char *argv[])
 {
   char ch;
   //we are storing first character of second parameter in below way
   ch=argv[2][0];
   


   if((ch >= 'a' && ch <= 'z') || (ch >= 'A' &&ch <= 'Z'))
   {
       if(ch=='a'||ch=='i'||ch=='o'||ch=='u'||ch=='e' ||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
	       printf("\n\n %c is Vowel ",ch);
	    else
	       printf("\n\n %c is Consonant ",ch);
   }
   else
      printf("\n\n %c is not character",ch);
    
   getch();
 }

Output

Enter character to check for Vowel or Consonant: a

a is not character

a is Vowel

While running above code from command line need to pass two arguments like “program name” “character” for e.g main.c i and then press enter. Program will read second string of command line and capture first character of that string and store it in variable “ch”. In If condition it is validating whether entered character is in between lower/upper case A to Z. After successful validation it is comparing characters with vowels if not matched then it shows output as consonant.

Any character which does not belong to A to Z, Program is treating it as “invalid input”.