Friday, 10 July 2015

Write a program to accept a line and check how many consonants and vowels are there in line


  
 
import java.io.*;
class Program_1
{
 public static void main(String args[]) throws IOException
 {
  String str;
  int vowels = 0, consonants = 0;
  char ch;
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  System.out.print("Enter a Line : ");
  str = br.readLine();
  for(int i = 0; i < str.length(); i ++)
  {
   ch = str.charAt(i);

   if(ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || 
   ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
    vowels ++;
   else if(Character.isAlphabetic(ch))
    consonants ++;   
  }
  System.out.println("Vowels : " + vowels);
  System.out.println("Consonants : " + consonants);  
 }
}
  

5 comments:

  1. what if there is a uppercase you should also add that

    ReplyDelete
  2. and the consonant will give wrong answer bcs its consider space also

    ReplyDelete
  3. there is error at "else if(Character.isAlphabetic(ch))"

    ReplyDelete