Write a function that reads an integer n and several lines of text (sentences between 150 – 200 characters). The text will be printed n characters per line, by adding extra spaces as evenly as possible.
For example:
Enter an integer (maximal number of chars per line): 10
Enter some sentences (between 150 – 200 chars): Your favorite forums are a dynamic listing of forums you're subscribed to as well as the the top forums that you post in. You currently don't have any favorite forums. You can visit the forum index for a listing of all forums.
Formatted text:
"Above paragraph will split for each line contains 10 characters"

I have a idea such we will define a counter(n) then set an for loop that run from 0 to n, then when it reached to less than n, we will insert a new line character '\n' to the position [n++]. After that, we will reset the for loop and for the next time, we have to start at inserted position until initial array of string still don't reach '\0'

I still have a problem at the step insertion, I really don't know how to come with syntax. Can you help me dear friends? Thank you so much

All you have to do is print the lines on the screen. You don't have to insert anything into the actual text. in a loop from 0 to length of line, print one character, increment a count. If the counter == maxlength, print '\n' and reset the counter back to 0. That's all there is to it.If you have to do word-wrap then the algorithm will become a little more complicated.

All you have to do is print the lines on the screen. You don't have to insert anything into the actual text. in a loop from 0 to length of line, print one character, increment a count. If the counter == maxlength, print '\n' and reset the counter back to 0. That's all there is to it.If you have to do word-wrap then the algorithm will become a little more complicated.

Can you give more details how to align 2 hand side?
Thank you so much.

I too am having a hard time understanding what you're asking.

are you saying that you want the lines to be printed "n" number of characters wide, such that extra spaces are inserted between words in order that both the left- and right-hand sides are justified margins?

Example:
n=12
Sentence = "Now is the time for all good men to come to the aid of their country."

Result:

Now  is  the
time for all
good  men to
come  to the
aid of their
country.

is that what you're asking?

Edited 6 Years Ago by jephthah: n/a

I too am having a hard time understanding what you're asking.

are you saying that you want the lines to be printed "n" number of characters wide, such that extra spaces are inserted between words in order that both the left- and right-hand sides are justified margins?

Example:
n=12
Sentence = "Now is the time for all good men to come to the aid of their country."

Result:

Now  is  the
time for all
good  men to
come  to the
aid of their
country.

is that what you're asking?

Sorry about that, my English is not really good!
You are thinking in common with mine. Thus, Would you like to give me some hints? Thanks

You have your string, and your width. You keep adding char's from your string (including the spaces), until you equal your width.

If your last char is a space, or period (punctuation of some kind), then you're OK, as is.

If you're in the middle of a word, then you have to start subtracting back to the last char of the previous word, to find a stopping point for that line.

In J's example, "time" was just too big to fit into the first line of 12 char's. So you back up and stop at the first space/punctuation mark, less than 12.

"Now is the time" == 15 char's total (count the spaces between words, also).

Back up to the last char of the previous word - 'e' in the, at 10 count. Subtract 10 from 12, and you need to give out two spaces. You have two spaces between words to give them out into:
between Now and is, and between is and time.

Give out the extra spaces (maybe from left to right), until you run out. If you have too many to give out only 1 space per word gap, then you have to start giving out a second one.

Do that until you're out of spaces to give out. Your margins will be equal, then.

Edited 6 Years Ago by Adak: n/a

This article has been dead for over six months. Start a new discussion instead.