Case studies over structure types and strings Instructor – Gokcen Cilingir Cpt S 121 (July 21,...

8
Case studies over structure types and strings Instructor – Gokcen Cilingir Cpt S 121 (July 21, 2011) Washington State University

Transcript of Case studies over structure types and strings Instructor – Gokcen Cilingir Cpt S 121 (July 21,...

Page 1: Case studies over structure types and strings Instructor – Gokcen Cilingir Cpt S 121 (July 21, 2011) Washington State University.

Case studies over structure types and strings

Instructor – Gokcen Cilingir

Cpt S 121 (July 21, 2011)

Washington State University

Page 2: Case studies over structure types and strings Instructor – Gokcen Cilingir Cpt S 121 (July 21, 2011) Washington State University.

Problem 1

Problem statement: Write a program that reads a paragraph from a text file and produces the list of words read from the file in the order of last word first. Output should be written to both standard output and to a file.

Page 3: Case studies over structure types and strings Instructor – Gokcen Cilingir Cpt S 121 (July 21, 2011) Washington State University.

Problem 1 (cont’d)

Functional decomposition:

int readWord(FILE * infile, char *word) – takes string formatted input from a file and places it into the given string.

void readWordList(FILE * infile, char wordList[][MAX_WORD_LENGTH], int *size) – repetitively takes string formatted input from a file (until EOF) and places them into the given string array. Returns as output parameter the size of the list.

void customPrint (char *str, FILE * outfile) – writes the given string both to standard output and to the given file.

Page 4: Case studies over structure types and strings Instructor – Gokcen Cilingir Cpt S 121 (July 21, 2011) Washington State University.

Problem 2

Problem statement: Extend the program you’ve written for problem 1 such that each word is displayed stating it’s length. For example, if the word is “Write”, than “Write (5)” should be displayed.

Page 5: Case studies over structure types and strings Instructor – Gokcen Cilingir Cpt S 121 (July 21, 2011) Washington State University.

Problem 3

Problem statement: Extend the program you’ve written for problem 2 such that each word is right-stripped from punctuation.

Additional function to write:void rightStripFromPunctuation(char *str) –

Alters the given string such that it is stripped from preceding punctuation

Page 6: Case studies over structure types and strings Instructor – Gokcen Cilingir Cpt S 121 (July 21, 2011) Washington State University.

Problem 4Problem statement: Write a program that

takes nouns and forms their plurals on the basis of these rules: a. If noun ends in “y” remove the “y” and

add “ies” b. If noun ends in “s” , “ch”, or “sh”, add

“es” c. In all other cases, just add “s”

Print each noun and its plural. Try the following data:

chair dairy boss circus fly dog church clue dish

Page 7: Case studies over structure types and strings Instructor – Gokcen Cilingir Cpt S 121 (July 21, 2011) Washington State University.

Problem 5Problem statement: Numeric addresses for computers on the

international network internet are composed of four parts ,separated by periods of the form

xx.yy.zz.mm

where xx, yy, zz, and mm are positive integers. Locally, computers are usually known by a nickname as well.

Design a program to process a list of internet addresses, identifying all pairs of computers from the same locality.

Create a structure type called address_t with components for the four integers of an internet address and a fifth component in which to store an associated nickname of 10 characters.   

Your program should read a list of up to 100 addresses and nicknames from an input file.

Page 8: Case studies over structure types and strings Instructor – Gokcen Cilingir Cpt S 121 (July 21, 2011) Washington State University.

Problem 5 (cont’d)Sample Data

111.22.3.44                    platte555.66.7.88                    wabash111.22.5.66                    green0.0.0.0                            none

The program should display a list of messages identifying each pair of computers from the same locality-that is , each pair of computers with matching values in the first two components of the address. In the messages, the computers should be identified by their nicknames.