Java program to count no of consonants in a string

import java.util.*;
class CountConsonants
{
public static void main(String s[])
{
Scanner sc = new Scanner(System.in);
int count = 0;
char ch;
System.out.println("Enter The String To Count No. Of Consonants : ");
String str = sc.nextLine();
for(int i=0;i<str.length();i++)
{
ch = str.charAt(i);
count++;
if((ch == 'a') || (ch == 'e') || (ch == 'i') || (ch == 'o') || (ch == 'u') || (ch ==' '))
{
count--;
}
}
System.out.println("The no of Consonants in given string is : " + count);
}
}

Comments