-->

Hello,This is me!

ITEngineer8995

Professional Coder Coding is my passion

Tuesday, June 2, 2020

Java Program To Check Vowels and Consonants in a String

  • June 02, 2020
  • by

Java Program To Check Vowels and Consonants in a String

In this Java Program, we calculate the vowels and consonants in a string.

The alphabet contains 26 letters, among them A, E, I,  O, U (small and upper case) are known as vowels while the remaining 21 letters are known as consonants.  

Logic :  switch(ch)
            {
                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':
                case 'A':
                case 'E':
                case 'I':
                case 'O':
                case 'U': isVowel = true;
                  
            }
            if(isVowel == true)
            {
                System.out.println(ch+" is a Vowel");
                
            }
            else 
            {
                if((ch>='a' && ch<='z')||(ch>='A' && ch<='Z'))
                    System.out.println(ch+" is a Consonant");
                else
                    System.out.println("Input is not an alphabet");
            }

Step 1 : Import java.util.Scanner

Step 2 : Create variable isVowel and initialize it to false

Step 3 : Ask the user to enter a character by using Scanner

Step 4 : Store the user entered character in the variable ch

Step 5 : Close the Scanner to avoid memory leak

Step 6 : Apply the logic by using the switch case

Step 7 : Apply if else condition 

Step 8 : Display the result by using System.out.print

Program :

import java.util.Scanner;

public class VowelConsonent 
    {
        public static void main(String[] args)
        {
            boolean isVowel = false;
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter a character:");
            char ch = sc.next().charAt(0);
            sc.close();
            switch(ch)
            {
                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':
                case 'A':
                case 'E':
                case 'I':
                case 'O':
                case 'U': isVowel = true;
                  
            }
            if(isVowel == true)
            {
                System.out.println(ch+" is a Vowel");
                
            }
            else 
            {
                if((ch>='a' && ch<='z')||(ch>='A' && ch<='Z'))
                    System.out.println(ch+" is a Consonant");
                else
                    System.out.println("Input is not an alphabet");
            }
        }
    
}

Output:

Enter a character :
A
A is a Vowel

Enter a character :
P
P is a Consonant

Enter a character :
9
Input is not an alphabet



For execution of the same do visit my YouTube Channel:

A blog full of codes of various languages like Java, .net, C, C++ etc with their outputs and some other IT stuff.

0 comments:

Post a Comment

ITEngineer8995
India

SEND ME A MESSAGE

Search This Blog

Powered by Blogger.

Pages