Meep

Meep

  • NA
  • 4
  • 794

c program using functions

May 18 2017 11:00 AM

In magazines the print is justified (spread out) to fit into columns. Write a program that reads in three lines of text and justifies the lines to fit a column width of 45. Your program should include three functions:

- readtext that reads in one line of text of not more than 45 characters, and returns its length

- gapscount that counts and returns the number of gaps between words in a line of text

- display that prints out a justified version of a line of text (see sample output below)

When your program is running, the screen should look something like this:

Enter a line of text: Hi to you world!

Enter a line of text: No answer was the stern reply

Enter a line of text: Goodbye cruel world

123456789012345678901234567890123456789012345

Hi to you world!

123456789012345678901234567890123456789012345

No answer was the stern reply

123456789012345678901234567890123456789012345

Goodbye cruel world

Justification of a line of text must be done by:

i) counting the number of gaps between words (In the above examples, there are 3, 5 and 2 gaps respectively.) ii) sharing any additional spaces between the gaps by:

- printing an equal share of the additional spaces in each gap

- if there are any left over spaces that can’t be shared equally, printing them one per gap until they run out. (In the first example, there are 11, 11 and 10 spaces between words. In the second example there are 5, 4, 4, 4 and 4 spaces between words. In the last example there are 14 and 14 spaces between words.

Notes:

1. The program must keep reading in a line of text until user input is a valid length.

2. Assume that the lines of text will have more than one word in them.

3. Assume that all gaps between words in input involve only one space.

4. Do not store justified strings. Simply display text strings, one character at a time, in the justified format.

5. Note the header line consisting of 1234567890.... Include this as a useful way to check your result.