Thomas Ding

Thomas Ding

  • NA
  • 5
  • 812

C program with strings

Oct 4 2017 2:43 AM
In magazines the print is justified (spread out) to fit into columns. Write a program that reads in 3 lines of text and justifies the lines to fit a column width of 40. Your program should include three functions:
 
- readtext that reads in one line of text of not more than 40 characters
- gapscount that counts and returns the number of gaps between words in one line of text
- display that prints out one line of text, justified (see sample output below)
 
When your program is running, the screen should look something like this:
 
Enter a line of text: If it weren’t for your gumboots,
 
1234567890123456789012345678901234567890
 
If it weren’t for your gumboots,_
 
Enter a line of text: where would ya be?
 
1234567890123456789012345678901234567890
 
where would ya be?_
 
Enter a line of text: You’d be in the hospital
 
1234567890123456789012345678901234567890
 
You’d be in the hospital_
 
Justification of a line of text must be done by:
 
i) counting the number of gaps between words (In the above lines of input, there are 5, 3 and 4 gaps.)
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 3, 3, 3, 3 and 2 spaces between words. In the second example there are 9, 8 and 8 spaces between words. In the last example there are 5 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 1234567890.... Include this as a useful way to check your result.
6. Functions must have at least one string parameter (you may choose to use other parameters too).
7. Notice the position of the cursor after each justified line.