Laboratory Exercise 4

CMP 230-2: Programming Methods I
Lehman College, City University of New York
22 September 1999

Today's lab is on the String class and its methods. Reading Section 2.2 in the textbook will make this lab easier.

Changing Words

Write a program that reads in a line of text and then outputs that line of text with the first occurrence of "hate" changed to "love". For example, a possible sample dialogue might be:

Enter a line of text:
I hate you
I have rephrased that line to read:
I love you

(The words in bold are those entered by the user.)

This program is a modification of the StringDemo program on p 61. One way to approach this lab is to type in the StringDemo program and make sure it compiles. Then, modify it to search for the word, "hate" and replace it with "love". After that, modify the program to let the user enter the phrase.

Make sure that your program are written in good style and have good documentation (include your name and the honesty pledge). After demonstrating your programs to Professor St. John, mail her copies at

If there is still time left in the lab, work on Homework 4.