Write a program to enter a sentence. Calculate total Vowel, Space, Consonant, and word.

Write a program to enter a sentence. Calculate total Vowel, Space, Consonant, and word.

Solution.
import java.io.*;
/**
 * class SentenceCounter
 *
* Vowel, Space, Consonant  and word counter program
 */
class SentenceCounter
{
                public static void main(String args[])throws IOException
                {
                                String st="";
                                int i,l,v,c,sp;
                                char a;
                                v=c=sp=0;
                                InputStreamReader in=new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(in);
                                System.out.println("Enter a sentence:");
                                st=br.readLine();
                                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));
                }
}

Output:
Enter a sentence:
the quick brown fox jumps over a lazy hungry dog
Total Vowel=13
Total Space=9
Total Consonent=27
Total Words=10

No comments:

Post a Comment