Algorithm


  1. As we know, vowels are - Lowercases: a, e, i, o, u and upper case vowels are - Uppercases: A, E, I, O, U
  2. Take a character.
  3. Check if the character is any of the upper case and lower case character or not
  4. If it's in these series, then it's a vowel, otherwise it's not a vowel

Code Examples

#1 Code Example with C Programming

Code - C Programming

#include <stdio.h>
int main()
{
  char ch;
 
  printf("Enter a character to check vowel: \n");
  scanf("%c", &ch);

  // Checking if the character is vowel - Check lower case and upper case

  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);
     
  return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
Enter a character to check vowel: a
Enter a character to check vowel: c

Output

x
+
cmd
a is a vowel.
c is not a vowel.

#2 Check with Vowel and constant

Code - C Programming

#include <stdio.h>
int main()
{
  char ch;
 
  printf("Enter a character to check vowel: \n");
  scanf("%c", &ch);

  // Checking if the character is vowel - Check lower case and upper case
  if ((ch >= 'a' && ch  < = 'z') || (ch >= 'A' &&ch <= 'Z')) {
      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 constant.\n", ch);
  } else {
     printf("%c is a not a vowel or not a constant.\n", ch); 
  }
     
  return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
Enter a character to check vowel: a
Enter a character to check vowel: b
Enter a character to check vowel: 4

Output

x
+
cmd
a is a vowel.
b is a constant.
4 is a not a vowel or not a constant..

#3 Code with more descriptive and organized Vowel and constant check

Code - C Programming

#include <stdio.h>
int main()
{
  char ch;
 
  printf("Enter a character to check vowel: \n");
  scanf("%c", &ch);

  // Checking on by one first -  Check is lower case
  int isLowerCaseVowels = (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
  
  // Check is upper case
  int isUperCaseVowels = (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U');
  
  // Check if is alphabet or not
  int isAlphabet = (ch >= 'a' && ch  < = 'z') || (ch >= 'A' &&ch <= 'Z');
  
  if ( isAlphabet ) {
      if ( isLowerCaseVowels || isUperCaseVowels )
        printf("%c is a vowel.\n", ch);
      else
        printf("%c is a constant.\n", ch);
  } else {
     printf("%c is a not a vowel or not a constant.\n", ch); 
  }
     
  return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
Enter a character to check vowel: a
Enter a character to check vowel: b
Enter a character to check vowel: 4

Output

x
+
cmd
a is a vowel.
b is a constant.
4 is a not a vowel or not a constant..

#4 Check vowel and consonant using Function

Code - C Programming

#include <stdio.h>


/**
 * Checks if a character is vowel or not 
 * 
 * @params char character
 * 
 * @return int 1 if vowel, 0 if constant
 */
int checkVowel(ch) {
    // Checking on by one first -  Check is lower case
  int isLowerCaseVowels = (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
  
  // Check is upper case
  int isUperCaseVowels = (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U');
  
  if ( isLowerCaseVowels || isUperCaseVowels ){
      return 1;
  }
  
  return 0;
}

int main()
{
  char ch;
 
  printf("Enter a character to check vowel: \n");
  scanf("%c", &ch);

  // Check if is alphabet or not
  int isAlphabet = (ch >= 'a' && ch  < = 'z') || (ch >= 'A' &&ch <= 'Z');
  
  if ( isAlphabet ) {
      if ( checkVowel(ch) )
        printf("%c is a vowel.\n", ch);
      else
        printf("%c is a constant.\n", ch);
  } else {
     printf("%c is a not a vowel or not a constant.\n", ch); 
  }
     
  return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
Enter a character to check vowel: a
Enter a character to check vowel: b
Enter a character to check vowel: 4

Output

x
+
cmd
a is a vowel.
b is a constant.
4 is a not a vowel or not a constant..
Advertisements

Demonstration


  1. Just take input character in C programming - scanf("%c", &ch);
  2. Then in if condition, check if it's the specific character manually
Next
Appending into a File in C Programming