Dremendo Tag Line

Input sentence and print each word with number of vowels present in it in Java

String - Question 9

In this question, we will see how to input a sentence and print each word along with the number of vowels present in it in Java programming. To know more about string click on the string lesson.

Q9) Write a program in Java to input a sentence and print each word along with the number of vowels present in it.

Program

import java.util.Scanner;

public class Q9
{
    public static void main(String args[])
    {
        String s,w="";
        char ch;
        int i,j,vc;
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a sentence");
        s=sc.nextLine()+" ";  	// add a space so that we can get the last word

        for(i=0; i<s.length(); i++)
        {
            if(s.charAt(i)!=' ')
            {
                //concatenates the character until we find a space
                w=w+s.charAt(i);
            }
            else
            {
                vc=0;
                for(j=0; j<w.length(); j++)
                {
                    ch=Character.toLowerCase(w.charAt(j));
                    if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
                    {
                        vc++;
                    }
                }
                System.out.println(w+" = "+vc);
                w="";
            }
        }
    }
}

Output

Enter a sentence
My hobby is to read novels and watch movies
My = 0
hobby = 1
is = 1
to = 1
read = 2
novels = 2
and = 1
watch = 1
movies = 3
video-poster