insert newlines into long string 
Author Message
 insert newlines into long string

Hello, i am writing a C function to insert newlines
at the appropriate space to  break a long string into
lines of roughly eighty characters. Could anyone make
this more concise:

#include <stdio.h>

char line[] = "a very long line needs to go here";

char *p = line;
int i,j = 0;

int main()
{
    while (*p++) {
      i++;
      if (i == 80) {
         while (*p-- != ' ') {
            i--;
         }
         for (i = i + j;j<=i;j++) {
             putchar(line[j]);
         }
         putchar('\n');
         i = 0;
      }
    }

Quote:
}



Sun, 26 Jun 2005 01:32:46 GMT  
 insert newlines into long string

Quote:

> Hello, i am writing a C function to insert newlines
> at the appropriate space to  break a long string into
> lines of roughly eighty characters. Could anyone make
> this more concise:

> #include <stdio.h>

> char line[] = "a very long line needs to go here";

> char *p = line;
> int i,j = 0;

> int main()
> {
>     while (*p++) {
>       i++;
>       if (i == 80) {
>          while (*p-- != ' ') {
>             i--;
>          }

Undefined behaviour, if no ' ' is found.

   int j, i = 0;

   while ((i += LEN) < sizeof line - OFF) {
     for (j = 0; j < OFF; j++)
       if      (line[i - j] == ' ') { line[i - j] = '\n'; break; }
       else if (line[i + j] == ' ') { line[i + j] = '\n'; break; }
   }

   puts(line);

Jirka



Sun, 26 Jun 2005 02:56:54 GMT  
 insert newlines into long string

Quote:

> Hello, i am writing a C function to insert newlines
> at the appropriate space to  break a long string into
> lines of roughly eighty characters. Could anyone make
> this more concise:

> #include <stdio.h>

> char line[] = "a very long line needs to go here";

> char *p = line;
> int i,j = 0;

> int main()
> {
>     while (*p++) {
>       i++;
>       if (i == 80) {
>          while (*p-- != ' ') {
>             i--;
>          }
>          for (i = i + j;j<=i;j++) {
>              putchar(line[j]);
>          }
>          putchar('\n');
>          i = 0;
>       }
>     }
> }

    Before making it concise, work on making it correct.

    First problem: What happens if line[] contains, say,
one hundred and one 'x' characters with no spaces?  The
loop that searches backward for the rightmost space will
buzz merrily off the beginning of the string and into the
Twilight Zone.

    Second problem: What happens if line[] is short?  The
loop that searches forward will stop before it hits the
eightieth character, and nothing will get printed at all.
Similarly, what happens if line[] is long but if the final
segment is short?

    Third problem: It seems peculiar that you carefully
locate a candidate space but then output it *and* the
newline.  Outputting the newline instead of the space
seems more logical.

    Fourth problem (maybe mine, not yours): The way you're
keeping track of the lengths of things confuses the{*filter*}ens
out of me, and I can't convince myself the arithmetic is
right.  I'm confident I *could* do so at need, but since
there are better ways to do this sort of thing I'm just
not motivated to push through with the exercise.

    Fifth problem: The main() function is properly declared
as returning an `int' value, but doesn't actually do so.
(Pedants: Yes, C99 allows this.  It's still a bad idea.)

    I'd suggest taking a somewhat different approach
using three pointers.  One will keep track of the start
of the current line or line section, one will search
forward looking for spaces or for the end of the buffer,
and one will remember the rightmost space found so far:

        char *bol;  /* start of current line */
        char *cur;  /* scan position */
        char *spc;  /* rightmost space */

    Initialize all three to point to the beginning of
the buffer, then start scanning `cur' forward until you
find a space or the final '\0'.  When you find either of
these, calculate `cur - bol' to find how long a line you'd
have if you included everything up to but not including
the `cur' position.  If it's too long, output all the
characters from `bol' up to but not including `spc' (if
`bol == spc' you've got an excessively long word and can
decide what to do with it), then output a newline and set
`bol = ++spc' (thus skipping the space character).  Now
if the original scan with `cur' found a space, resume
scanning.  If it found the '\0' instead, output the
fragment starting at `bol' as the final (short) line.

    I hope that's a clear enough description; since the
problem sounds as if it might be homework I'm reluctant
to supply actual code.  Good luck!

--



Sun, 26 Jun 2005 02:29:35 GMT  
 insert newlines into long string

Quote:

> Hello, i am writing a C function to insert newlines
> at the appropriate space to  break a long string into
> lines of roughly eighty characters. Could anyone make
> this more concise:

> #include <stdio.h>

> char line[] = "a very long line needs to go here";

> char *p = line;
> int i,j = 0;

Why are these not local to main?

Quote:

> int main()
> {
>     while (*p++) {
>       i++;
>       if (i == 80) {
>          while (*p-- != ' ') {
>             i--;
>          }
>          for (i = i + j;j<=i;j++) {
>              putchar(line[j]);
>          }
>          putchar('\n');
>          i = 0;
>       }
>     }
> }

Well, you might just replace the ' 's with '\n's, and eventually
output the whole line.  
However there are insecurities.  The line must be modifiable to do
that (OK above).  What if there are no blanks for a stretch of 80
chars or more?  What if there are tab separators?  What if the
line already contains embedded '\n's?  What if there are embedded
control sequences, such as '\b' or ANSI terminal escape
sequences?  Maybe there are none of these things because of known
earlier processing.

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!



Sun, 26 Jun 2005 03:37:58 GMT  
 insert newlines into long string
Warning: this code was not tested and may contain logical errors...

 char * breakup(char * input) { char * found = 0; char * backup = 0;
 char * last_chance = 0; char * p; int i;

    for(p = input, i = 0; *p; ++p, ++i) { if( *p == '\n' ) { found = p;
    } else if(*p == ' ') { backup = p; } else if(!isalnum(*p)) {
    last_chance = p; }

      //...do the deed...

        if( i% 80 == 0 ) { if(found) { i = (int)(p - found); } else
             if(backup) { *backup = '\n'; i = (int)(p - backup); } else
             if(last_chance) { *last_chance = '\n'; i = (int)(p -
             last_chance); }

        //...otherwise, do nothing...

       found = backup = last_chance = 0; } } return input; }

--
posted via MFF :  http://www.MainFrameForum.com - USENET Gateway



Sun, 26 Jun 2005 03:28:22 GMT  
 insert newlines into long string

Quote:

> Hello, i am writing a C function to insert newlines
> at the appropriate space to  break a long string into
> lines of roughly eighty characters. Could anyone make
> this more concise:

> #include <stdio.h>

> char line[] = "a very long line needs to go here";

> char *p = line;
> int i,j = 0;

> int main()
> {
>     while (*p++) {
>       i++;
>       if (i == 80) {
>          while (*p-- != ' ') {
>             i--;
>          }
>          for (i = i + j;j<=i;j++) {
>              putchar(line[j]);
>          }
>          putchar('\n');
>          i = 0;
>       }
>     }
> }

Make it /more/ concise? It's already /too/ concise. Others have already
taken the trouble to decode it elsethread. Personally, I'm waiting for the
widescreen version, so that I can see what's going on without having to get
out my metaphorical magnifying glass.

It's bad enough when programmers write easy-to-write code, rather than
easy-to-read code. By trying to make the program more concise, you are
actually making it not only harder to read, but /also/ harder to write!

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Sun, 26 Jun 2005 04:23:45 GMT  
 insert newlines into long string
All the points you've made about the snippet are true.
However, i'm only writing a concise function to handle
the best case scenario. Which is a line with more
than 80 chars and spaces within. The reason i was writing
this code was to compare the equivalent perl code.
I also like your suggested algorithm at the end
of the post...thanks...

sf

Quote:


>> Hello, i am writing a C function to insert newlines
>> at the appropriate space to  break a long string into
>> lines of roughly eighty characters. Could anyone make
>> this more concise:

>> #include <stdio.h>

>> char line[] = "a very long line needs to go here";

>> char *p = line;
>> int i,j = 0;

>> int main()
>> {
>>     while (*p++) {
>>       i++;
>>       if (i == 80) {
>>          while (*p-- != ' ') {
>>             i--;
>>          }
>>          for (i = i + j;j<=i;j++) {
>>              putchar(line[j]);
>>          }
>>          putchar('\n');
>>          i = 0;
>>       }
>>     }
>> }

>     Before making it concise, work on making it correct.

>     First problem: What happens if line[] contains, say,
> one hundred and one 'x' characters with no spaces?  The
> loop that searches backward for the rightmost space will
> buzz merrily off the beginning of the string and into the
> Twilight Zone.

>     Second problem: What happens if line[] is short?  The
> loop that searches forward will stop before it hits the
> eightieth character, and nothing will get printed at all.
> Similarly, what happens if line[] is long but if the final
> segment is short?

>     Third problem: It seems peculiar that you carefully
> locate a candidate space but then output it *and* the
> newline.  Outputting the newline instead of the space
> seems more logical.

>     Fourth problem (maybe mine, not yours): The way you're
> keeping track of the lengths of things confuses the{*filter*}ens
> out of me, and I can't convince myself the arithmetic is
> right.  I'm confident I *could* do so at need, but since
> there are better ways to do this sort of thing I'm just
> not motivated to push through with the exercise.

>     Fifth problem: The main() function is properly declared
> as returning an `int' value, but doesn't actually do so.
> (Pedants: Yes, C99 allows this.  It's still a bad idea.)

>     I'd suggest taking a somewhat different approach
> using three pointers.  One will keep track of the start
> of the current line or line section, one will search
> forward looking for spaces or for the end of the buffer,
> and one will remember the rightmost space found so far:

> char *bol;  /* start of current line */
> char *cur;  /* scan position */
> char *spc;  /* rightmost space */

>     Initialize all three to point to the beginning of
> the buffer, then start scanning `cur' forward until you
> find a space or the final '\0'.  When you find either of
> these, calculate `cur - bol' to find how long a line you'd
> have if you included everything up to but not including
> the `cur' position.  If it's too long, output all the
> characters from `bol' up to but not including `spc' (if
> `bol == spc' you've got an excessively long word and can
> decide what to do with it), then output a newline and set
> `bol = ++spc' (thus skipping the space character).  Now
> if the original scan with `cur' found a space, resume
> scanning.  If it found the '\0' instead, output the
> fragment starting at `bol' as the final (short) line.

>     I hope that's a clear enough description; since the
> problem sounds as if it might be homework I'm reluctant
> to supply actual code.  Good luck!



Sun, 26 Jun 2005 07:51:06 GMT  
 insert newlines into long string

Quote:

> All the points you've made about the snippet are true.
> However, i'm only writing a concise function to handle
> the best case scenario. Which is a line with more
> than 80 chars and spaces within. The reason i was writing
> this code was to compare the equivalent perl code.
> I also like your suggested algorithm at the end
> of the post...thanks...

Don't toppost.  Especially around here.

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!



Sun, 26 Jun 2005 11:05:46 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. formatting long strings using sprintf() without including tabs or newlines

2. convert string to long, and long to string

3. programmatically inserting newline?

4. char size (was long long long long long int)

5. format string for long long

6. long long long long integers

7. Hashing long input strings into short output strings

8. How do you get a newline in a textbox and string in C#

9. string newline replace

10. length of strings w. newlines in them

11. Hashing long input strings into short output strings

12. stripping newlines from strings

 

 
Powered by phpBB® Forum Software