Question
*This needs to be written in c programming using arrays. * Write a program with the file name lab5_problem2.c that asks the user to enter
Write a program with the file name
lab5_problem2.c
that asks the user to enter a sentence
ended by a period and prints out the number of times each character appears in the sentence, only
for the characters that occur at least once. Uppercase and lowercase version of a letter should be
counted toward the same letter. The program should function as follows (the items underlined are
to be entered by the user):
Enter a sentence (end by .): This is a short sentence.
Occurrences of a: 1
Occurrences of c: 1
Occurrences of e: 3
Occurrences of h: 2
Occurrences of i: 2
Occurrences of n: 2
Occurrences of o: 1
Occurrences of r: 1
Occurrences of s: 4
Occurrences of t: 3
Algorithm design process
1. What variables are needed and what type?
a. A variable to store the
character
entered by the user.
b. An array of integers to store the number of
occurrences
for each character.
What should be the size of the array? How many characters there are in the
alphabet? Dont forget to properly initialize the array with what should we
initialize it?
c. Any other variables that you need? Maybe an integer counter used in a loop to
print the number of occurrences at the end.
2. Get input from user
a. Print out the text to prompt the user
b. Set up a loop (
while
,
do-while
) to read in character by character until a
.
is found.
3. Inside the loop
a. In order to handle upper/lower case situations, we should convert each character
read to lowercase (use the
tolower
function from the string library).
b. For every character read, increment the corresponding value in the occurrences
array. How do we find the index associated with each character? Hint: character
a
should be associated with the first element of the array (index 0), character
b
with index 1 and so on. We can get the index by subtracting
a
from the
value of the current character. This index can now be used to properly increment
the values in the occurrences array.
4. Print the results.
a. Set up a
for
loop that iterates through the occurrences array and prints out the
statistics. How do we print the
a, b, c
... letters in the output string?
We can obtain the character by adding the value of the index to character
a
(this is the offset value for all the subsequent letters). Thus 0 will become
a
, 1
will be
b
and so on.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started