Computer Science 2311
Fall 2007
Lab Assignment 4

This assignment has two parts. You will write two separate programs. When you are done, print the compiler listing for both programs and turn them in.

Strings

A character (of type Char) is written inside single quotes. For example, 'y' is the lower-case y character. A string (of type String) is sequence of zero or more characters. You can write strings inside double quotes, such as "abc". A string is not the same thing as a character. For example, "a" is not the same as 'a'.

Some operations on strings

length(x)

length(x) yields the number of characters in string x. For example:

length("abc") = 3

prefix?(x, y)

prefix?(x, y) yields true if string x is a prefix of string y. For example:

prefix?("rat", "rather") = true
prefix?("rat", "rat") = true
prefix?("rat", "the rat") = false

x # n

x # n is the n-th character in string n. For example:

"travel" # 1 = 't'
"travel" # 2 = 'r'

drop n x

drop (n) (x) yields the string that you get by removing the first n characters from string x. For example:

drop (2) ("wrist") = "ist"
drop (1) ("travel") = "ravel"
Notice that you write drop (n) (x), not drop(n,x).

infile(name)

infile(name) produces the contents of file name. For example, expression infile("stuff.txt") yields the contents of file "stuff.txt", as a string.

readString()

readString() reads a string (up to a white-space character) from the keyboard, and yields that string. The string can be enclosed in double-quote marks. In that case, it reads up to the next double-quote mark.

Program 1

Write a program that asks the user for a file name and a word. It should tell whether the named file contains that word. The word can be part of another word. For example, if the word is ter and the program contains mystery, the program should say that the file does contain the word. The program should not keep the case of letters the same. For example, it will not consider the word cat to have occurred if it sees Cat, because the letters are not exactly the same.

A sample session is as follows. Parts in blue are typed by the user.

  What is the name of the file?  rabbit.txt
  What word shall I search for? hare
  File rabbit.txt does contain hare.  

Hint. There are various ways to do this, and this hint describes just one. You are doing a search. Use either a loop or recursion to do the search. Write a function that tests whether a word occurs in a text string. What happens if the text is an empty string? (You can write an empty string "" in a program.) Try checking whether the word is a prefix of the text. If not, look for the string in what you get by dropping the first character.

Program 2

In honor of International Talk Like a Pirate Day, this program reads a file and shows what the file looks like after replacing some words. It should replace words is and are by be, and replace Is and Are by Be. (This is not much, but it will do for now.) Only replace words that are preceded and followed by a white-space character, defined to be either a space or a newline character. Suppose that file rant.txt contains the following.

 There are too many rabbits here!  Is there any way to get 
 rid of some?
Here is a sample session.
  What file shall I read?  rant.txt

 There be too many rabbits here!  Be there any way to get 
 rid of some?

Hint. Try an approach similar to what you did for the previous program. But print the characters that you see during the search. What should you do if "is" is a prefix of what is left of the text?