In: Computer Science
Input a phrase from the keyboard. For example, input could be:
Today is a rainy day, but the sun is shining.
Your program should count (and output) the number of "a"s in the phrase, the number of "e"s, the number of "i"s , the number of "o"s, the number of "u"s, and the total number of vowels.
Note: The number of "a"s means the total of uppercase and lowercase "a"s etc.
Also: the vowels are a,e,i,o,u.
In the above example your output should be:
The phrase is "Today is a rainy day, but the sun is shining."
The number of a's: 4
The number of e's: 1
The number of i's: 5
The number of o's: 1
The number of u's: 2
The total number of vowels is: 13
Code language: java
Code:
import java.util.*;
class Vowel{
public static void main(String[] args) {
Scanner s =new
Scanner(System.in);
System.out.println("Enter a
phrase");
String str=s.nextLine();
int no_a=0;
int no_e=0;
int no_i=0;
int no_o=0;
int no_u=0;
int total=0;
for(int
i=0;i<str.length();i++){
if(((str.charAt(i))=='a')||((str.charAt(i))=='A')){
no_a++;
total++;
}
else
if(((str.charAt(i))=='e')||((str.charAt(i))=='E')){
no_e++;
total++;
}
else
if(((str.charAt(i))=='i')||((str.charAt(i))=='I')){
no_i++;
total++;
}else
if(((str.charAt(i))=='o')||((str.charAt(i))=='O')){
no_o++;
total++;
}else
if(((str.charAt(i))=='u')||((str.charAt(i))=='U')){
no_u++;
total++;
}
else{
//do nothing
}
}
System.out.println("The number of a's: "+no_a);
System.out.println("The number of e's: "+no_e);
System.out.println("The number of i's: "+no_i);
System.out.println("The number of o's: "+no_o);
System.out.println("The number of u's: "+no_u);
System.out.println("Total Vowels are: "+total);
}
}
Output:
Code Screenshot:
Code snippet:
//as we are using scanner
import java.util.*;
//class name is Vowel
class Vowel{
public static void main(String[] args) {
Scanner s =new Scanner(System.in);
System.out.println("Enter a phrase");
//taking input
String str=s.nextLine();
//initializing all vowel counts
int no_a=0;
int no_e=0;
int no_i=0;
int no_o=0;
int no_u=0;
int total=0;
//a loop that traverses the string
for(int i=0;i<str.length();i++){
//if the character is a
if(((str.charAt(i))=='a')||((str.charAt(i))=='A')){
no_a++;
total++;
}
//if it is e
else if(((str.charAt(i))=='e')||((str.charAt(i))=='E')){
no_e++;
total++;
}
//if it is i
else if(((str.charAt(i))=='i')||((str.charAt(i))=='I')){
no_i++;
total++;
}
//if it is o
else if(((str.charAt(i))=='o')||((str.charAt(i))=='O')){
no_o++;
total++;
}
//if it is u
else if(((str.charAt(i))=='u')||((str.charAt(i))=='U')){
no_u++;
total++;
}
else{
//do nothing
}
}
System.out.println("The number of a's: "+no_a);
System.out.println("The number of e's: "+no_e);
System.out.println("The number of i's: "+no_i);
System.out.println("The number of o's: "+no_o);
System.out.println("The number of u's: "+no_u);
System.out.println("Total Vowels are: "+total);
}
}