Write a program to enter the sentence and count the occurrence of vowels in each word.
Example: Enter sentence : Sharada Mandir celebrates Golden Jubilee Year
Output:
Sharada : Number of vowels=3
Mandir : Number of vowels=2
celebrates : Number of vowels=4
Golden : Number of vowels=2
Jubilee : Number of vowels=4
Year : Number of vowels=2
Answers
Required Answer:-
Question:
- Write a program to enter the sentence and count the occurrence of vowels in each word.
Solution:
Here comes the códe.
1. In Java.
import java.util.*;
public class Words {
public static void main(String[] args) {
String s, x[];
int i;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a Sentence: ");
s=sc.nextLine();
x=s.split(" ");
for(i=0;i<x.length;i++)
System.out.println("Number of vowels in "+x[i]+" = "+countVowels(x[i]));
sc.close();
}
static int countVowels(String s) {
int c=0, i;
s=s.toLowerCase();
for(i=0;i<s.length();i++) {
char ch=s.charAt(i);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
++c;
}
return c;
}
}
2. In Python.
def countVowels(x):
c=0
for letter in x:
if letter in "aeiouAEIOU":
c+=1
return c
s=input("Enter a Sentence: ")
x=s.split(" ")
for str in x:
print("Number of vowels in ",str," = ",countVowels(str))
Algorithm:
- START
- Accept the sentence.
- Split the sentence into words.
- Count vowels in each word and display it.
- STOP
Refer to the attachment for output ☑.