Lab 8 – Strings, Arrays of Strings, and File I/O

CPE101

Fall 2008

 

Updates and Corrections

·         None Yet!

Due Date:

By the end of your last lab of week 9 (unless your instructor tells you otherwise).

Objectives

·         To learn about strings.

·         To learn about arrays of strings.

·         To practice file I/O.

Overview

In this lab you will be writing some functions to work with strings, arrays of strings, and files.  Recall that a string is simply an array of characters terminated in the null character ‘\0’.  Thus, an array of strings is really a two dimensional array of characters.  An array meant to hold the names of five people might look like this:

 

int names[5][10];  /* five names, each with a max length of 9 */

 

‘B’

‘o’

‘b’

‘\0’

 

 

 

 

 

 

‘C’

‘e’

‘c’

‘i’

‘l’

‘i’

‘o’

‘\0’

 

 

‘D’

‘e’

‘r’

‘e’

‘k’

‘\0’

 

 

 

 

‘M’

‘e’

‘g’

‘a’

‘n’

‘\0’

 

 

 

 

‘M’

‘i’

‘c’

‘h’

‘a’

‘e’

‘l’

‘\0’

 

 

 

What is names[3]?

What is names[1][2]?

 

In this lab you will get more practice reading from a file.  This time you will be reading strings from the file.  Use the same endfile loop technique that you did in the find_sevens lab, described on pages 242-243.

Lab Requirements 

1.      In a file called lab8a.c write a program that prompts the user for a string and then prints the string reversed.  You may assume the word entered will not be more than 19 characters long.  Declare your string accordingly.

You must use a function called wordReverse to do the actual reversing.  This function should not return anything.  Its only parameter should be the string to reverse.  Do not make any assumptions about the length of the string.  You must use the null character to determine where the string ends.  You may use the strcpy function from string.h, but it is not required.

A sample run of the program is below.  User input is in bold for clarity.

Enter a word: midterm

The word reversed is: mretdim

 

2.      In a file called lab8b.c write a program that prompts the user for a filename, opens the file, and prints its contents to the screen one word per line. 

You must use a function called filePrint to do the actual printing.  The function should not return anything.  Its only parameter should be the name of the file to read and print.  The function should open the file, read words one at a time, and print them to the screen.  You do not need to store the words.  Simply read each word and print it one at a time until you reach the end of the file.  The function should not do any additional I/O.  The prompting of the file name should be done in main.

A sample run of the program is below.  User input is in bold for clarity.

Enter a file to print: test.txt

hi

there

i

am

a

file

to

test

lab8 

3.      In a file called lab8c.c write a program that prompts the user for a filename, opens the file, prints its contents to the screen, and then prints its contents to the screen in reverse.  The order of the words and the words themselves should be reversed.  Use the two functions you wrote in Part A and Part B.

You must use a function called filePrintReverse to do the actual printing.  The function should not return anything.  Its only parameter should be the name of the file to read and print.  The function should first read all the words in the file into an array of strings.  You may assume the file will not contain more than 100 words.  Reverse the characters in each individual word immediately after reading the word from the file.  After reading in the entire file.  Print the words to the screen in reverse order.  As with the filePrint function, this function should not do any I/O other than the printing of the file.

A sample run of the program is below.  User input is in bold for clarity.

Enter a file to reverse: test.txt

 

The file before reversing:

hi

there

i

am

a

file

to

test

lab8

 

The file reversed:

8bal

tset

ot

elif

a

ma

i

ereht

ih

Handing in Your Source Electronically . . .

As with our previous labs, you need to submit your source code electronically using the   handin   utility:

1.      If necessary, transfer your source (lab8.c) to vogon.

2.      Change directory (cd-command) to the directory containing the source file or files to hand in.

3.      Compile and test your source on vogon using the required compiler flags (-Wall -ansi -pedantic).

4.    Use the handin command being sure to handin your code to the appropriate grader account and directory (one of the following):

 

11:59 vogon ~$ handin gradercst lab08 lab8a.c lab8b.c lab8c.c