954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Newbie string problem

Hello. I have this problem:

I have a let's say [1000] char string and i want to split and print this on the screen on more lines, so that each and every line has MAXIM 43 characters. The string may contain spaces and the words would be printed on the next line if they cant fit on the current line...

I have no idea how to do that. I know how to show the letters from, lets say [100] to the end of the string, with StringName+100, but how can i show the string from 100 to 143?...

Thank you in advance.

Prahaai
Junior Poster in Training
59 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

you will need a second char array. copy the first 43 characters to this second char array array then print it. then print the remainder of the original array.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Use counter which will print the chars.

for(i=0; i<totalLengthOfString; i++)
{
   if(i != 0 && i % MAXIM == 0)
   {
      putchar('\n');
   }
   putchar(string[i]);
}

The second question also can solve with counter.

andor
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 

Use counter which will print the chars.

The second question also can solve with counter.

Yes, that is a better solution than mine -- doesn't require a second buffer.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Thank you very much, the idea is EXCELLENT.

(I had to include ostream for it to work. :p)

Prahaai
Junior Poster in Training
59 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You