Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Third International Conference Achids 2011 Daegu Korea April 2011 Proceedings Part 2 Lnai 6592

Authors: Ngoc Thanh Nguyen ,Chong-Gun Kim ,Adam Janiak

2011th Edition

3642200419, 978-3642200410

More Books

Students also viewed these Databases questions

Question

=+Is the guilt appropriate for the organization?

Answered: 1 week ago