Computer Science, asked by chayanikaniyogi, 10 months ago

Wap to input a string and print the word having maximum no. Of vowels. in java​

Answers

Answered by ʙᴇᴀᴜᴛʏᴀɴɢᴇʟ
4

Program to print a word with most number of vowels

java arrays string

I am unable to figure out what is wrong with this code. Whatever I provide as input, the code prints i as output. Any help is appreciated.

public class VowelClass{

public static void main(String args[]){

String input;

System.out.println("Please enter your sentence: ");

Scanner scan = new Scanner(System.in);

input = scan.nextLine();

int maxVCount = 0;

String mostVowels = null;

String[] words = input.split("");

for (String word : words) {

int vCount = 0;

word = word.toLowerCase();

for (int i=0; i<word.length(); i++){

char x = word.charAt(i);

if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u'){

vCount++;

}

}

if (vCount > maxVCount) {

maxVCount = vCount;

mostVowels = word;

}

}

System.out.println("Word with most vowels is:");

System.out.println(mostVowels);

}

}

Similar questions