Java program for vowels of each word of a sentence
The following program will print each word’s total vowels. In the following program we have taken a sentence and then first segregate each word. After then each word's vowels is calculated in the loop.
import java.util.*;
public class Sent2Word
{
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 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.length(); // length of
the word
for(j=0; j<l1;
j++)
{
ch1=st.charAt(j);
if(ch1=='A'
|| ch1=='E' || ch1=='I' || ch1=='O' || ch1=='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 dog
THE contain vowel=1
QUICK contain vowel=1
BROWN contain vowel=1
DOG contain vowel=1
More Java programs