Assignment Exercises

Exercise 1

Write a C program to read through an array of any type. Write a C program to scan through this array to find a particular value.

Exercise 2

Read ordinary text a character at a time from the program's standard input, and print it with each line reversed from left to right. Read until you encounter end-of-data (see below).

You may wish to test the program by typing

prog5rev | prog5rev

to see if an exact copy of the original input is recreated.

To read characters to end of data, use a loop such as either

char ch;
while( ch = getchar(), ch >= 0 ) /* ch < 0 indicates end-of-data */

or

char ch;
while( scanf( "%c", &ch ) == 1 ) /* one character read */

Exercise 3

Write a program to read English text to end-of-data (type control-D to indicate end of data at a terminal, see below for detecting it), and print a count of word lengths, i.e. the total number of words of length 1 which occurred, the number of length 2, and so on.

Define a word to be a sequence of alphabetic characters. You should allow for word lengths up to 25 letters.

Typical output should be like this:

length 1 : 10 occurrences
length 2 : 19 occurrences
length 3 : 127 occurrences
length 4 : 0 occurrences
length 5 : 18 occurrences
....

To read characters to end of data see above question.

Post a Comment

0 Comments