![]() |
![]() |
This topic demonstrates about how to find entered character or alphabet is either vowel or consonant using java program.
Vowel
A vowel is a speech sound made by allowing breath to flow out of the mouth, without closing any part of the mouth or throat.
Consonant
A consonant is a speech sound made by partially or completely blocking the flow of air through the mouth. or The words other than vowels are called as consonants.
Example
Here in above chart vowels are a,e,i,o,u and all others are consonants.
public class SwitchDemo { public static void main(String[] args) { char mychar = 'i'; String result; switch(mychar) { case 'a': result = "Vowel"; break; case 'e': result = "Vowel"; break; case 'i': result = "Vowel"; break; case 'o': result = "Vowel"; break; case 'u': result = "Vowel"; break; default: result = "Consonant"; break; } System.out.println("Given character is "+result); } }
Output
import java.util.Scanner; public class StringVowelsConsonants { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String str; int countVowels=0,countConso=0; System.out.print("Enter any string : "); str = scan.next(); for(int i=0; i< str.length();i++) { if(str.charAt(i)=='a'||str.charAt(i)=='e'||str.charAt(i)=='i' ||str.charAt(i)=='o'||str.charAt(i)=='u') { countVowels++; System.out.println("Vowel - "+str.charAt(i)); } else { countConso++; System.out.println("Consonant - "+str.charAt(i)); } } System.out.println("\nTotal Vowels : "+countVowels); System.out.println("Total Consonants : "+countConso); } }