Write a C program to enter a character through keyboard. Use switch-case and print appropriate message. Recognize the entered character whether it is a vowel or consonant or symbol.

Since, it is given in the program definition that we have to use switch-case statement. So, it is a good example to exercise switch-case concept in C language.

We will display 3 messages, one for all cases of vowel characters, another for all cases of consonant characters and last one for all other characters i.e. symbol. Thus, we can construct our logic for the program to find if a character is vowel or consonant or symbol.

Steps ๐Ÿชœ

To find if a character is Vowel or Consonant or Symbol

  1. Accept a character from the user and store the value in a variable ‘ch‘.
  2. Here, we will use switch-case statement to find out if the accepted character is a vowel, consonant or symbol.
  3. First, we will create 10 cases for vowels, 5 cases for each uppercase and lowercase. To simplify the code and make it more compact & concise, we will write just one printf() statement with message “It is a Vowel” for all cases of vowel characters.
  4. Same as for consonant, we will create cases for all consonants in both uppercase and lowercase, total 42 cases. Again, we will write just one printf() statement with message “It is a Consonant” for all cases of consonant characters.
  5. Now, write a printf() statement in default case section which displays the message “It is a symbol“, if any other character does not match the above cases for vowel and consonant.

This is how we find out if a character is Vowel or Consonant or Symbol. ๐Ÿ˜‰

Code ๐Ÿ’ป

How To find if a character is Vowel or Consonant or Symbol

// Find if a character is Vowel or Consonant or Symbol

#include<stdio.h>
#include<conio.h>

void main()
{
  char ch;
  clrscr();
  printf("Please enter a character: ");
  ch = getche();
  switch(ch)
  {
    // Vowels
    case 'a': case 'e': case 'i': case 'o': case 'u':
    case 'A': case 'E': case 'I': case 'O': case 'U':
    printf("\n\nIt is a Vowel.");break;
    // Consonants
    case 'b': case 'c': case 'd': case 'f': case 'g': case 'h': case 'j':
    case 'k': case 'l': case 'm': case 'n': case 'p': case 'q': case 'r':
    case 's': case 't': case 'v': case 'w': case 'x': case 'y': case 'z':
    case 'B': case 'C': case 'D': case 'F': case 'G': case 'H': case 'J':
    case 'K': case 'L': case 'M': case 'N': case 'P': case 'Q': case 'R':
    case 'S': case 'T': case 'V': case 'W': case 'X': case 'Y': case 'Z':
    printf("\nIt is a Consonant.");break;
    // Default: Symbol
    default: printf("\nIt is a Symbol.");
  }
  getch();
}

Accept character using scanf() instead of getche() function

// Find if a character is Vowel or Consonant or Symbol

#include<stdio.h>
#include<conio.h>

void main()
{
  char ch;
  clrscr();
  printf("Please enter a character: ");
  scanf("%c", &c);
  switch(ch)
  {
    // Vowels
    case 'a': case 'e': case 'i': case 'o': case 'u':
    case 'A': case 'E': case 'I': case 'O': case 'U':
    printf("\n\nIt is a Vowel.");break;
    // Consonants
    case 'b': case 'c': case 'd': case 'f': case 'g': case 'h': case 'j':
    case 'k': case 'l': case 'm': case 'n': case 'p': case 'q': case 'r':
    case 's': case 't': case 'v': case 'w': case 'x': case 'y': case 'z':
    case 'B': case 'C': case 'D': case 'F': case 'G': case 'H': case 'J':
    case 'K': case 'L': case 'M': case 'N': case 'P': case 'Q': case 'R':
    case 'S': case 'T': case 'V': case 'W': case 'X': case 'Y': case 'Z':
    printf("\nIt is a Consonant.");break;
    // Default: Symbol
    default: printf("\nIt is a Symbol.");
  }
  getch();
}

Example ๐Ÿ˜

Let’s assume, the user enters a character ‘a‘ and we know that there is a matching case, so it will display the message as following:

It is a Vowel.

If the user enters ‘X‘, it will look for matching case and will display the message as:

It is a Consonant.

It does not matter if the user enters any alphabet character in lowercase or uppercase. There is a separate case for each and every alphabet (both in lowercase and uppercase). Thus, it will look for the matching case and will display the appropriate message.

But, if the character is other than alphabet then it will match the default case & display the message as:

It is a Symbol.

For more C programs, click here.

Categorized in: