203k views
0 votes
Write a program that asks user to enter a string of characters, then computes and displays the number of occurrences of the vowel letters 'a', 'e', 'i', 'o', 'u', and 'A', 'E', 'I', 'O', 'U' without case sensitivity, i.e. using one single count for both 'a' and 'A'.

Code must use an integer array (size 5) to store the occurrence counts.

Code must use a character array (size 300) to store input string. Each user input can be up to 299 characters.

Hint:

1- Use getline to read input into a character array. Size of character array should be 1 more than the maximum input size because it needs to be terminated by a (invisible) NULL character.

The getline() function automatically puts a NULL character after the last character that it reads.

2- Use Sentinel loop to accept multiple input until input string length is 0 (empty string "" when user hits Return)

3- To get the length of a C string (that is a null-terminated character array), you can use the strlen() function if you include "cstring" library. Empty C string will have a length of 0.


Test input strings

HellO JavA.

Some miscellanEous IssUes At schoOl.

C++ is grEat languagE.

1 Answer

1 vote

Final answer:

The goal is to write a program that counts vowel occurrences in a string using an integer array to hold counts and a character array to store input. It involves looping, checking each character for vowels, incrementing counts, and displaying results.

Step-by-step explanation:

The task is to write a program that counts the number of vowel occurrences in a user-provided string. Vowels are considered irrespective of their case ('a' and 'A' are treated the same), and the count should be stored in an integer array of size 5, where each index corresponds to a vowel ('a', 'e', 'i', 'o', 'u'). The user's input, which can be up to 299 characters long, should be stored in a character array of size 300 to accommodate the NULL character appended by the getline() function. A sentinel loop is to be used for handling multiple inputs until an empty string is entered. The strlen() function from the "cstring" library can be used to determine the string's length.



To achieve the task, the program will:

  • Use a loop to repeatedly prompt the user for input.
  • Parse the input string character by character to identify vowels.
  • Increment the corresponding index in the occurrences array when a vowel is encountered.
  • Terminate the loop when an empty string is input.
  • Finally, display the total count of each vowel to the user.

Using an array to store these counts allows for efficient access and manipulation of the data, while the use of the getline() function streamlines the process of reading input strings. It should be noted that the character array must be large enough to contain the user's input plus the NULL character.