Answer to Question #295559 in Java | JSP | JSF for Scaler

Question #295559

Write a program to take a String as input then display the word which has highest number of vowels.

For example:

Enter a String

EDUCATION IS important

EDUCATION has highest number of vowels

2nd e.g.

Enter a String

Hello use dog

Hello and use has same highest number of vowels


1
Expert's answer
2022-02-09T10:40:53-0500
import java.util.*;


class App {


	static int countNumberVowelsInWord(String word) {
		int count = 0;
		for (int i = 0; i < word.length(); i++) {
			if (word.toLowerCase().charAt(i) == 'a' || word.toLowerCase().charAt(i) == 'e'
					|| word.toLowerCase().charAt(i) == 'i' || word.toLowerCase().charAt(i) == 'o'
					|| word.toLowerCase().charAt(i) == 'u') {


				count++;
			}
		}
		return count;
	}


	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter a String: ");
		String inputWords[] = in.nextLine().split(" ");
		int counters[] = new int[inputWords.length];
		int maxVowels = 0;


		for (int i = 0; i < inputWords.length; i++) {
			counters[i] = countNumberVowelsInWord(inputWords[i]);
			if (counters[i] > maxVowels) {
				maxVowels = counters[i];
			}
		}
		int count = 0;
		for (int i = 0; i < inputWords.length; i++) {
			if (counters[i] == maxVowels) {
				count++;
			}
		}


		if (count == 1) {
			for (int i = 0; i < inputWords.length; i++) {
				if (counters[i] == maxVowels) {
					System.out.print(inputWords[i]);
				}
			}
			System.out.print(" has highest number of vowels");
		} else {
			for (int i = 0; i < inputWords.length; i++) {
				if (counters[i] == maxVowels) {
					System.out.print(inputWords[i]);
					if (count > 1) {
						System.out.print(" and ");
					}
					count--;


				}


			}
			System.out.print(" has same highest number of vowels");
		}


		in.close();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS