Question: ***NEED PROGRAM TO BE WRITTEN IN #C*** Problem 1: Write a program with the file name lab5_problem1.c that asks the user to enter a sentence

***NEED PROGRAM TO BE WRITTEN IN #C***

Problem 1:

Write a program with the file name lab5_problem1.c that asks the user to enter a sentence ended by a period, a question mark, or an exclamation mark and prints out the number of space characters (spaces, new lines, tabs) in that sentence.

The program should function as follows (the items underlined are to be entered by the user):

Enter a sentence (end by . or ? or !):

This is a sample

sentence.

Number of space characters: 2

Number of new line characters: 2

Number of tabs: 1

Note: Above there is a new line before the first word of the sentence and after the word sample, there are two spaces (after is and after a) and a tab (after the word This).

Algorithm design process

What variables are needed and what type?

Need variables to store the count of characters, new_lines and tabs.

Any other variables that you need? A variable of type char to read in the input from the user.

Get input from user

Print out the text to prompt the user

Set up a loop (while, do-while) to read in character by character until a . or ? or ! are found. What should be the logical condition that will enable the loop to run until . or ? or ! are encountered?

Inside the loop

For every character read, see if it is a new line, space or tab, in order to increment the appropriate counter. Ignore any other characters. What is the best way to structure this? A cascaded if-else statement would be too long, so the best option would be a switch statement. Think about structuring the switch statement in the most compact way possible (group cases that go together and think of what should go in the default case).

At the end of the loop print out the number of spaces, new lines and tabs.

Problem 2:

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

What variables are needed and what type?

A variable to store the character entered by the user.

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?

Any other variables that you need? Maybe an integer counter used in a loop to print the number of occurrences at the end.

Get input from user

Print out the text to prompt the user

Set up a loop (while, do-while) to read in character by character until a . is found.

Inside the loop

In order to handle upper/lower case situations, we should convert each character read to lowercase (use the tolower function from the string library).

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.

Print the results.

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!