Rearrange Text To Fit On A Screen With Assumed Margins

I just had a quick question: How would I rearrange a given text to fit on a screen with assumed margins?

The parameters are: text[] ( the text to rearrange ), and lineLength ( the max number of characters per line )

The overall algorithm is: Start from the beginning of the text
// While the remaining text > length of a line
// Skip forward over a number of characters equal to the line length
// Search backwards for a space character ' '
// (hint: this is a nested loop)
// Replace the space with a newline character '\n'

So far I have:

int textLength = int(strlen(text)); // total number of characters
int position; // current array position
position = 0; // start at beginning

for ( position = 0; position < lineLength; position++ )
{
text[position];


while ( textLength - position > lineLength )
{
position = lineLength;
for ( position = lineLength; position >= 0; position-- )
{
if ( text[position] == ' ' )
{
text[position] = '\n';
}
}
}
}

return text;


but its not working. Any help would be greatly appreciated! Thanks!
Topic archived. No new replies allowed.