write a program to enter a string and print the word having maximum number of vowel
Answers
(1) First, take an input from the user.
(2) Split the sentence
(3) Take the first word of the sentence as the longest vowel containing word and the vowel count of the first word as the highest vowel count in a single word.
(4) Check every word in the sentence and as each value increases, keep changing the value of longest vowel word and the vowel count.
(5) Print the word with the highest vowels.
To find the number of vowels in a word, we will have:
vowels = 'aeiouAEIOU'
And we will be using the 'in' operator to find the number of vowels.
CODE:
def vowels(word):
vowels,count = 'aeiouAEIOU',0
for letter in word:
if letter in vowels:
count += 1
return count
vowel_lengths = []
sentence = input().split()
for word in sentence:
vowel_lengths.append(vowels(word))
max_length = max(vowel_lengths)
for y in sentence:
if vowels(y) == max_length:
print(y)
Answer:
JAVA:
Explanation:
import java.util.*;
class Brainly
{
static void main()
{
Scanner sc=new Scanner(System.in);
String s,max="",w="";
int i,c=0,f=0,l=0;
char x;
System.out.print("Enter a sentence:");
s=sc.nextLine();
s=s.trim();
s=s+" ";
for(i=0;i<s.length();i++)
{
x=s.charAt(i);
if(x!=' ')
{
w=w+x;
if(x=='a' || x=='A' || x=='e' || x=='E' || x=='i' ||
x=='I' || x=='o' || x=='O' || x=='u' || x=='U')
c++;
}
else
{
if(f==0)
{
max=w;
l=c;
f=1;
}
else
{
if(c>l)
{
l=c;
max=w;
}
}
w="";
c=0;
}
}
System.out.println("Word having maximum number of vowels:"+max);
}
}