Laboratory Assignment 2 Solution





In this lab you will be working with strings. You will be reading a line at a time from the keyboard. The function scanf is not appropriate for input, instead you will use the function fgets to read lines from the keyboard. You will perform input for some number of lines, then enter control-D (hold down the control key and press key d) to stop input. The input lines will be stored in an array of strings. You will be reversing words, counting letters, and counting words. For this program (and all programs), use small functions. Each function should easily t on a screen and have a speci c purpose.

Speci cs

Your code will be formatted similarly to prime.c discussed in class on April 8. In particular, your comments will be similar to those in prime.c, and you will print author and title information to the output stream similar to the information printed in prime.c. You will request that the user will input up to 100 lines of text. Your array of strings will be able to store 100 lines, each of length up to and including 100 characters. Once the input is complete, your program will

count the number of words in each line

count the number of each alphabetic character in each line (case does not matter, ’a’ and ’A’ are both the letter a).

reverse each word in each line

A word is de ned as a sequence of non-white space characters. For our purposes, white space is a space, a tab, or a newline. Since you are working on individual lines, there will be no words separated by a newline in a line.

For example, if an input line is:

is. Think a! lIne?

there are 4 words (I am not counting letters except that there are 3 occurrences of the letter i or I).

The line with words reversed is:

.si knihT !a ?enIl

Your output will consist of:

Each line in your array preceded by its line number (start the numbering at 1). Note that all words will be reversed from the input.

For each line number, print the line number; the number of words; and the count of each alphabetic character.

print the total number of words and the count of each alphabetic character that was input.

Make sure to clearly label all output.


Submit your source code via Blackboard by 11:59:59p.m. on Tuesday April 16. This lab is worth 20 points.


error: Content is protected !!