8 Program to count number of vowels and consonant from an input String in Java.

Vowel.java

import java.util.Arrays;

class Vowel
{
public static void main(String args[])
{

String str = args[0];
char[] c = str.toCharArray();

int len = str.length();
int v=0,con=0;

System.out.println("String="+Arrays.toString(c));

for(int i=0 ; i < len ; i++)
{
if( c[i] == 'a' || c[i] == 'e' || c[i] == 'i' || c[i] == 'o' || c[i] == 'u' )
v++;
else if( c[i] == 'A' || c[i] == 'E' || c[i] == 'I' || c[i] == 'O' || c[i] == 'U' )
v++;
else
con++;
}

System.out.println("\n\n Vowels="+v);
System.out.println("\n\n Consonant="+con);

}
}


Output:  



Previous
Next Post »