Write a program that asks the user for a word or phrase and then prints out the
ID: 3780560 • Letter: W
Question
Write a program that asks the user for a word or phrase and then prints out the number of vowels in the word or phrase. We will considervowels to be only the letters a, e, i, o, and u. We will not consider the lettery to be a vowel. Remember to handle both uppercase and lower case letters. You program should contain at least 2 methods: the main method and a method for counting the number of vowels. If you get the first part working, modify your program to allow the user to enter multiple words. Use a sentinel value such as the letter 'q' or the word "quit' to signal the end of user input.Explanation / Answer
package org.students;
import java.util.Scanner;
public class VowelsCount {
public static void main(String[] args) {
//Scanner class object us used to read the inputs entered by the user
Scanner sc=new Scanner(System.in);
//Declaring variables
int vowelCount=0;
//This loop continues to execute until the user enters either 'q' or "quit"
while(true)
{
//Getting the string entered by the user
System.out.print("Enter the String :");
String str=sc.nextLine();
//Check whether the user entered string is "q" or "quit"
if(str.equals("q") || str.equals("quit"))
{
System.out.println(":: Program Exit ::");
break;
}
else
{
//calling the method by passing the user entered string as argument
vowelCount=countVowels(str);
//displaying the no of vowels in the string
System.out.println("No of vowels in the String are :"+vowelCount);
}
}
}
//this method will find the no of vowels in the string
private static int countVowels(String str) {
//Declaring variables
int vowelCount=0;
//this for loop will count the number of vowels
for (int i=0; i<str.length(); ++i)
{
if(str.charAt(i)=='a'||str.charAt(i)=='e'||str.charAt(i)=='i'||str.charAt(i)=='o'||str.charAt(i)=='u'
||str.charAt(i)=='A'||str.charAt(i)=='E'||str.charAt(i)=='I'||str.charAt(i)=='O'||str.charAt(i)=='U')
{
//counting the number of vowels
vowelCount++;
}
}
return vowelCount;
}
}
___________________________
Output:
Enter the String :hello
No of vowels in the String are :2
Enter the String :how are you mary
No of vowels in the String are :6
Enter the String :q
:: Program Exit ::
_________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.