Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement a program in C++ that stuffs text from a plain text file into neatly arranged paragraphs with a particular maximum allowed line length. You

Implement a program in C++ that stuffs text from a plain text file into neatly arranged paragraphs with a particular maximum allowed line length.

You are to implement the following function, whose job it is to do the stuffing:

 int stuff(int lineLength, istream& inf, ostream& outf); 

You may name the parameters something else, but there must be three parameters of the indicated types in the indicated order. The parameters are

  • an int with the desired maximum line length
  • an already-opened input source you will read from (probably a file the caller opened).
  • an already-opened output destination you will write to (probably either cout or an output file the caller created)

The function must return an int with one of the following values:

  • 0 if is successful (i.e., neither of the following problems occurs)
  • 1 if any input word portion (defined below) is longer than the maximum line length
  • 2 if the desired maximum line length is less than 1

In the case of the error situation that would cause the function to return 2, it must return 2 without writing any output.

Your stuff function may, of course, call other functions you write to help it do its job. You can test your stuff function by calling it from a main routine of your choosing. Our grading tool will ignore your main routine by renaming it to something harmless. If you wish, your main routine could prompt the user for the file names and the line length. Alternatively, it could hardcode the names of the files to use and prompt for only a line length. Do whatever works best for you to help you test your function.

We use the following definitions in the stuffing rules:

A word is a sequence of non-whitespace characters not immediately preceded or followed by a non-whitespace charcater. (The function isspace tells you if a character is a whitespace character.) The string "swan's nest" contains two words, swan's and nest. Because of immediately preceding or following non-whitespace, swan, wa, 's, and nes are not words of that string.

A word can be viewed as a sequence of one or more word portions. The first word portion in a word starts at the start of the word; subsequent word portions in a word start just after the last character of the previous word portion. The last character of a word portion is the first hyphen at or after the start of that word portion, or the end of the word if there is no hyphen after the start of that word portion. Here are examples, including some pathological ones:

Word Word portions

Thames Thames

so-called so- and called

come-as-you-are come-, as-, you-, and are

so--called so-, -, and called

so- so-

-so -, and so

Here are the stuffing rules:

Fit as many word portions in an output line as you can without exceeding the line length. Output lines may well end up being different lengths, giving a "ragged-right" effect; your stuffer will not try to right-justify the text.

Words in an output line must normally be separated by one blank. However, if the last character of an output word is a period or a question mark, it must be separated from any following word on that output line by two blanks. The last word on an output line must not be followed by any blanks. The first word portion on an output line must not be preceded by any blanks. Blanks must not appear on an output line within any word. The last line of output must end with a newline, and there must be no output lines (not even empty ones) after the last word of the last paragraph. For example, if the last word in the input is bye, then the last four characters your function would write are 'b' 'y' 'e' ' '. That does not produce an empty last line. Writing this would produce an empty last line, in violation of this rule: 'b' 'y' 'e' ' ' ' '.

Notice that this rule implies that if a word containing a hyphen is broken at a hyphen to fit on an output line, the hyphen must be the last character of one output line, and the character after the hyphen must be the first character of the next output line.

If a word portion is longer than the line length, as much as will fit must be on an output line by itself. The rest of that word portion must begin the next output line (and, of course, is subject to similar splitting if it's too long). If this situation ever occurs, your function continues stuffing, but it must eventually return 1 instead of 0 to its caller. Notice that this is the only situation where a word is allowed to be split across lines other than at a hyphen.

The input word #P# is not to be processed as a word according to the above rules; instead, it indicates a paragraph break. The first word in the input following a paragraph break will be the first word of a new paragraph in the output. If a paragraph has already been output, the new paragraph must be separated from the one that precedes it by an empty line (i.e., a line with no characters other than the terminating newline). The very first output paragraph must not be preceded by an empty line. The very last output paragraph must not be followed by an empty line. Two or more consecutive #P# words in the input are treated as just one paragraph break. Notice that banjo#P# is one eight-character word; it does not cause a paragraph break, because in that string, #P# is not a word because of the immediately preceding non-whitespace character o.

Notice that these rules imply that if the line length is valid but the input contains no words, stuff must return 0 without writing any output whatsoever.

Your stuff function and any functions you write that it calls directly or indirectly must not use any std::string objects. If you need to use a string, use a C string. Your function may assume that no input line will be 140 or more characters long (i.e., we will not test it with input lines that long).

If the first parameter of stuff is greater than 300, your implementation of stuff must stuff the text using the indicated line length, returning 0 or 1 as appropriate (i.e., your algorithm doesn't impose any upper limit on the output line length).

(This project may not use any C++ strings, only C strings)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

=+5. What are their resources?

Answered: 1 week ago