Java program to find total vowel in each word of a sentence
This program will print the total vowel of each word in a given sentence. We have taken a sentence in the 'st' variable and find the total length of the sentence. After then a for loop started and pick a word. Then find the length of the word and then find the vowels. Then print the word with a total number of vowels.
Vowel finder program
import java.util.*;
public class PrintEachWordFromSent
{
public static void main(String args[])
{
int i, l, l1, j;
String st, word;
int v;
// Initialize the variable to 0
v = 0;
word = "";
char ch, ch1;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence :");
st = sc.nextLine();
st = st + " ";
l = st.length(); // Total length of the String
for(i=0; i<l ;i++)
{
ch = st.charAt(i);
if(ch != ' ')
{
word = word + ch;
}
else
{
l1 = word.length();
for(j=0;j<l1;j++)
{
ch1 = st.charAt(j);
if(ch1 == 'A' || ch1 == 'a' ||ch1 == 'E' ||ch1 == 'e' ||ch1 == 'I' ||ch1 == 'i' ||
ch1 == 'O' ||ch1 == 'o' ||ch1 == 'U' ||ch1 == 'u' )
v++;
}
System.out.println("Total Vowels contain in " + word + "=" + v);
word="";
v = 0;
}
}
}
}
Output
Enter a sentence :
today is very cold out there
Total Vowels contain in today=2
Total Vowels contain in is=1
Total Vowels contain in very=2
Total Vowels contain in cold=2
Total Vowels contain in out=1
Total Vowels contain in there=2