Vowel, Space, Consonant and word counter Java program

Java program for Vowel consonant and word counter

In this program we are going to find out total vowel, consonant and total word in a sentence. Here we enter a sentence. Then first check vowel and then space. Here we count the space for total words. Add one to total space for determining total words and for consonant subtract total characters of the string to the summation of space and vowel as follows:
consonant = length of string - ( total vowels + total space)
 c=l-(v+sp);
//Vowel, Space, Consonant  and word counter program
import java.util.*;
class SentenceCounter
{
public static void main(String args[])
{
String st="";
int i,l,v,c,sp;
char a;
v=c=sp=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence:");
st=sc.nextLine();
l=st.length();
for(i=0;i<l;i++)
{
a=st.charAt(i);
if(a=='a' || a=='A' || a=='e' || a=='E' || a=='i' || a=='I' || a=='o' || a=='O' || a=='u' || a=='U')
v++;
if(a==' ')
 sp++;

}
c = l-(v+sp);
System.out.println("Total Vowel = " +v);
System.out.println("Total Space = " +sp);
System.out.println("Total Consonent = " +c);
System.out.println("Total Words = " + (sp+1));
}
}


SHARE THIS
Previous Post
Next Post