Write a program to read a string and count the vowels (“a”, “e”, “i”, “o”, and “
ID: 3931129 • Letter: W
Question
Write a program to read a string and count the vowels (“a”, “e”, “i”, “o”, and “u”). The program should provide a count for each vowel and a total count of vowels. The program should ensure that the vowels are counted for both upper and lower-case. Test the program on a series of different input values and verify that the output is correct for those input values.
Output shall be a formatted list of the format:
X occurs YYY times. Where X is each of the vowels and YYY is the number of occurrences in the input string.
So far I have this but I need help on how to display how many times each vowel occours.
program vowel
implicit none
INTEGER :: vowels=0
INTEGER :: i
CHARACTER :: letter
CHARACTER (len=1000) :: line
read '(a)', line
do i=1, 1000
letter = line(i:i)
select case (letter)
case ('A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u')
vowels = vowels + 1
end select
end do
write (*,*) "A occurs", vowels, "times"
end program vowel
Explanation / Answer
//Tested on Eclipse
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
public class Vowels {
public static void main(String args[]) {
/* creating HashMap for storing vowels and count */
TreeMap<String, Integer> map = new TreeMap<String, Integer>();
/* Inserting vowels into treemap */
map.put("a", 0);
map.put("e", 0);
map.put("i", 0);
map.put("o", 0);
map.put("u", 0);
/*
* creating instance variable of Scanner class for taking input from
* console
*/
Scanner scanner = new Scanner(System.in);
/*
* prompt for user input
*/
System.out.println("Please enter the String");
String input = scanner.nextLine();
/* closing scanner object */
scanner.close();
/* Converting into char array */
char character[] = input.toCharArray();
/* Iterating character array */
for (int i = 0; i < character.length; i++) {
String val = character[i] + "";
if (val.equalsIgnoreCase("a") || val.equalsIgnoreCase("e") || val.equalsIgnoreCase("i")
|| val.equalsIgnoreCase("o") || val.equalsIgnoreCase("u")) {
/* If vowels already exist in tree map */
if (map.containsKey(val.toLowerCase())) {
Integer value = map.get(val.toLowerCase());
map.put(val.toLowerCase(), ++value);
} else {
map.put(val.toLowerCase(), 1);
}
}
}
Set<String> keys = map.keySet();
for (String key : keys) {
System.out.print(key + "- " + map.get(key) + " ");
}
}
}
/********************output*******************/
Please enter the String
HEllo sam HOw are You. I am fine.
a- 3
e- 3
i- 2
o- 3
u- 1
Thanks a lot
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.