Manipulating Strings - Assignment


The Problem

Replacing placeholders in a piece of text is useful in many contexts. For example, in generating form letters:

Text with Placeholders Values for Placeholders Merged text
Dear <name>:
The Zippy Company would like to thank you for the resume you sent us on <date>, however, ...
<name> = John Smith
<date> = January 10th
Dear John Smith:
The Zippy Company would like to thank you for the resume you sent us on January 10th, however, ...

or for other things, like playing Mad Libs:

Text with Placeholders Values for Placeholders Merged text
The <adjective-1> <noun-1> ran over the <noun-2> very <adverb-1> ... <noun-1> = rooster
<noun-2> = octopus
<adjective-1> = urban
<adverb-1> = quaintly
The urban rooster ran over the octopus very quaintly ...


The Program

For this assignment, you will write a simple program that can merge a paragraph with values for a set of placeholders.

The program should work as follows:

% mergetext
Number of placeholders (no more than 10)? 2

Placeholder #1 (no spaces)? <first-name>
Placeholder #1 value (no spaces)? Robert

Placeholder #2 (no spaces)? <last-name>
Placeholder #2 value (no spaces)? Billings

Now enter the paragraph that uses the above placeholders.  Enter the
end-of-file character (i.e., Ctrl-D [UNIX] or Ctrl-Z [MS]) when you are
done:

     We would like to welcome <first-name> <last-name> to the wonderful
family of door-to-door representatives for the Raingo Barbecue
Item-of-the-Month Club. <first-name> -- you should be receiving your
sales kit in just a matter of days and will then begin your long
door-to-door career! But first, begin with Chapter 1 in the attached
pamphlet, "Learning to Smile Even When the Customer Slams the Door in
Your Face"!
<end-of-file>

The formatted paragraph is:

We would like to welcome Robert Billings to the wonderful family of
door-to-door representatives for the Raingo Barbecue Item-of-the-Month
Club.  Robert -- you should be receiving your sales kit in just a matter of
days and will then begin your long door-to-door career!  But first, begin
with Chapter 1 in the attached pamphlet, "Learning to Smile Even When the
Customer Slams the Door in Your Face"!

Note that formatting the paragraph not only involves replacing the placeholders, but also reformatting so that lines wrap properly.


Your Task

You must write this program that merges a paragraph of text with values for a set of placeholders. It must adhere to the following constraints:

Hints: Note that the whitespace in the original version of the paragraph is ignored. Thus, you can format lines only being concerned about the current and previous word that you are processing. Be sure to replace placeholders before you determine how a word affects line wrapping and the spacing between words.

Prepare this program, adding all appropriate comments and using attractive and readable formatting conventions. Call the program mergetext.cpp.


BU CAS CS - Manipulating Strings - Assignment
Copyright © 1993-2000 by Robert I. Pitts <rip@bu.edu> All Rights Reserved.