Advertisement
Shailrshah

Number of Vowels and Consonants in a String

Sep 8th, 2013
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.95 KB | None | 0 0
  1. import java.util.Scanner;
  2. class alphabetType{
  3.     public static void main(String args[]){
  4.         int consonants=0, vowels=0,others = 0;
  5.         System.out.print("Enter a String: ");
  6.         Scanner sc = new Scanner(System.in);
  7.         String input = sc.nextLine();
  8.         input = input.toLowerCase();
  9.         for(int i = 0; i < input.length(); i++){
  10.             switch(input.charAt(i)){
  11.  
  12.                 case 'a': case'e':case 'i': case'o':
  13.                 case 'u': vowels++; break;
  14.  
  15.                 case'b':case'c':case'd':case'f':
  16.                 case'g':case'h':case'j':case'k':
  17.                 case'l':case'm':case'n':case'p':
  18.                 case'q':case'r':case's':case't':
  19.                 case'v':case'w':case'x':case'y':
  20.                 case'z': consonants++; break;
  21.                
  22.                 default: others++;
  23.             }
  24.         }
  25.         System.out.println(consonants+" consonants & "+vowels+" vowels present. "+others+" other characters present.");
  26.     }
  27. }
  28.  
  29. //Output:-
  30. //C:\Java>java alphabetType
  31. //Enter a String: Thanks for visiting my Pastebin!
  32. //19 consonants & 8 vowels present. 5 other characters present.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement