Lisa b

Lisa b

  • NA
  • 31
  • 16.2k

C program with strings

Apr 26 2013 4:35 AM

If you look at a newspaper you will see that the writing is justified to fit into the columns.  Write a program

that reads in the width of the columns in a newspaper and then a line of text.  Justify the line of text to fit into

a column of that width.  When your program is running, the screen should look something like this:

Enter the width of the column: 40

Enter a line of text: Good morning how are you?

12345678901234567890123456789012345678901234567890...

Good          morning           how           are            you?

The justification is done by counting the number of gaps in the text.  In the above example, there are 4 gaps.  Then each gap must have spaces added to it.  The number of extra spaces must be shared out as evenly as possible.  In the above example, the first three gaps have 5 spaces each and the last gap has 4 spaces.

1.  If the text is longer than the column then you must report an error – don't try and break it into two lines!

2.  Assume that the text will have more than one word in it.

3.  Note the header line consisting of 123456789012345678....  this is useful to check your result.

You can make this header line as long as you like – 70 spaces would be a useful length.


Answers (1)