Write a program that determines the percentage of vowels in a sentence by counti
ID: 3549896 • Letter: W
Question
Write a program that determines the percentage of vowels in a sentence by counting the number of vowels in a sentence and dividing it by the total number of characters in that sentence. 1. Create a Java program that asks the user to input a sentence in any language, counts the number of vowels in the sentence, divides it by the total number of characters in the sentence, and displays (in the console window using System.out.println) the percentage of vowels. A vowel is a, e, i, o, and u (don't worry about y being a vowel sometimes). 2. Add another loop so that the program repeatedly asks the user for a sentence and prints the percentage of vowels in it. Make this an infinite loop, so that the program never stops asking for a sentence to process. You can do this by while(true) { ...rest of the program... }Explanation / Answer
Hi,
Please find the program below.
import java.util.Scanner;
public class VowelCount {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
float count =0;
while (true) {
System.out.println("Enter a Sentence");
String sentence = sc.nextLine();
sentence = sentence.toLowerCase();
for (int i = 0; i < sentence.length(); i++) {
if (sentence.charAt(i) == 'a' || sentence.charAt(i) == 'e'
|| sentence.charAt(i) == 'i'
|| sentence.charAt(i) == 'o'
|| sentence.charAt(i) == 'u') {
count++;
}
}
System.out.println("Percentage of vowels" + count/sentence.length());
}
}
}
Output:
Enter a Sentence
aeiou aeiou abcd aeiou
Percentage of vowels0.72727275
Enter a Sentence
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.