Strings, File I/O

You are free to use a single or multiple files for this project. If you are putting all functions in a single file, make sure that more abstract functions are defined first.

Text justification is spacing of the text block so that both right and left side of the text have a straight edge. This is accomplished by adding spaces so that every line is exactly the same length. You are to implement simplified justification rules as follows. Every line should be exactly 80 characters long (keep this value in a named constant). To get to this line length, extra space is first added after punctuation marks. You need to consider only these marks: ,.!?: That is, every punctuation mark may be followed by two spaces unless it is the last character in the line or it is followed by a non-whitespace charater. That is, space may not be added in the middle of this string: 10.20.30 If the extra spaces after punctuation marks are added and the line is still less than 80 characters, additional spaces are added after random words in the line. There is no need to ensure that the extra spaces are spread uniformly. Randomization is sufficient. A line that is shorter than 40 characters is considered the end of a paragraph and is not justified. Hyphenation (breaking up of words) between lines is not required.

  1. Line Justification. Study code folding in MSVS described here. As you work on the below project, demonstrate to the lab instructor code folding and unfolding. Create a project titled Lab7_LineJustify. Write a program that asks the user to input a string and then prints this string out in 80 characters according to the above justification rules. You may assume that the user always inputs the line that is less than 80 characters. Below is an example dialog:
    input line: 
    Contrary to popular belief: Lorem Ipsum is not simply random text.
    your line justified:
    Contrary to  popular belief:   Lorem Ipsum  is not   simply  random    text.
    

    Hints: Use getline() function to get user input. See this example for its operation. To locate spaces and punctuation characters, consider this code. This code demonstrates how multiple occurrences of the same symbol may be found. String size() function determines the number of characters in a string. This code demonstrates string modification. Consider using find_first_of(white space characters) to locate the white space following selected position.

    To randomly insert spaces, consider the following approach

    if there is white space in the line then
       continue until the string is justified
          randomly pick a position between 0 and string size
          locate the first white space following this position 
             (make sure it is not the end of the string)
          insert a space there
    
  2. Text Justification. Create a project titled Lab7_TextJustify. Repeat the assignment assuming that the input comes from a file such as this one. The input file is guaranteed to have lines under 80 characters. You do not need to move words between lines. You can save the example file in your computer (cut-and-paste is acceptable) in file unjustified.txt. In Visual Studio, you can just add this file to your project just as you do with source and header files. The output of your program should go to a different file named justified.txt. For the above example file, the output file would look like this. Again, you can add this file to the project and observe its contents.

    Hints: You may reuse the code from the first part of the lab assignment. Putting it in a separate function will make the code more modular. This code demonstrates how to read a file line-by-line.

Milestone: For the first assignment, insert spaces after punctuation marks.

Make sure your programs adhere to proper programming style. Submit your projects to the subversion repository. Do not forget to verify your submission on the web.