Java program to display the number of vowels in each word of the Sentence

Java program to display the number of vowels in each word of the Sentence

 

Java program to display the number of vowels in each word of the Sentence

 In the following the java program, we calculate vowels of each word of a sentence, entered by the user. The sentence is taken in the st variable. After that word will be saved in word variable in the following block

        for(i=0; i<l; i++)

        {

            ch=st.charAt(i);

            // if space is come then goto else calculate each words vowel

            if(ch!=' ')

            {

                word=word + ch;

            }

The word is build by each character taken till space is encounter. And then count the vowels from the word and print.

 

import java.util.*;

public class EachWordsVowelPrint

{

    public static void main(String args[])

    {

        String st, word="";

        char ch,ch1;

        int v;      // variable for vowel

        int c;      // variable for Consonents

        int w;      // variable for word

        int i,j;    // for loop

        int l,l1;   // l is for length of sentence and for word

        v=c=w=0;    // Initialization

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a sentence:");

        st=sc.nextLine();

        st=st + " ";    // one space add to the sentence

        //st=st.toUpperCase();

        l=st.length();  // length of the sentence

        for(i=0; i<l; i++)

        {

            ch=st.charAt(i);

            // if space is come then goto else calculate each words vowel

            if(ch!=' ')

            {

                word=word + ch;

            }

            else

            {

                l1=((word.trim()).length());   // length of the word               

                for(j=0; j<l1; j++)

                {

                    ch1=word.charAt(j);                    

                    switch(ch1)

                    {

                        case 'A' :

                        case 'a' :

                        case 'E' :

                        case 'e' :

                        case 'I' :

                        case 'i' :

                        case 'O' :

                        case 'o' :

                        case 'U' :

                        case 'u' :

                        v++; 

                    }                 

                }

                // print the word total vowels

                System.out.println(word + " contain vowel= " + v);

                // Initialize the words and v for vowel

                word = "";   

                v=0;

            }

        }

    }

}

Output

Enter a sentence:

the quick brown fox jumps over a lazy hungry dog

the contain vowel= 1

quick contain vowel= 2

brown contain vowel= 1

fox contain vowel= 1

jumps contain vowel= 1

over contain vowel= 2

a contain vowel= 1

lazy contain vowel= 1

hungry contain vowel= 1

dog contain vowel= 1

 

More Java programs 



SHARE THIS
Previous Post
Next Post