string newline replace 
Author Message
 string newline replace

hi

I'm having trouble writing this function which takes a string and
counts the number of characters it takes from each newline. The count
will be zero when it sees a newline. If it reaches 97, it will insert
a newline. Here's what I have so far:

int countnewline(char *string)
{
      int i, ncounter;
      for(i=0;i<strlen(string);i++){
          if(string[i]=='\n')
                {
                     ncounter=0;
                }
           else if(ncounter==97)
                {
                     ncounter=0;
                     string[i]=='\n';
                }
           else
                     ncounter++;

Quote:
}



Thu, 27 Jan 2005 15:25:56 GMT  
 string newline replace

Quote:

>           else if(ncounter==97)
>                {
>                     ncounter=0;
>                     string[i]=='\n';
>                }

My guess is that you meant string[i] = '\n'; ... this has the effect of
replacing whatever character is in that position with a newline, so if
that position had something you might want to keep, it would be best to
try a different approach.

-Tim Miller



Thu, 27 Jan 2005 15:30:28 GMT  
 string newline replace

Zimmerman) wrote in comp.lang.c:

Quote:
> hi

> I'm having trouble writing this function which takes a string and
> counts the number of characters it takes from each newline. The count
> will be zero when it sees a newline. If it reaches 97, it will insert
> a newline. Here's what I have so far:

> int countnewline(char *string)
> {
>       int i, ncounter;

You need to initialize ncounter somewhere, either where you define it:

   int i, ncounter = 0;

Quote:
>       for(i=0;i<strlen(string);i++){

...or in the initialization of the loop:

    for (i=0, ncounter=0;i<strlen(string);i++)

Quote:
>           if(string[i]=='\n')
>                 {
>                      ncounter=0;
>                 }
>            else if(ncounter==97)
>                 {
>                      ncounter=0;
>                      string[i]=='\n';

The problem here is that this does not insert a newline, it overwrites
a character of the string with a newline.  The character that was
originally there was lost.

Quote:
>                 }
>            else
>                      ncounter++;
> }

A few more comments:

1.  Don't call strlen() at each reinitialization of the loop.  Define
a variable of type size_t and assign it by calling strlen() before the
start of the loop.  Since your code modifies the string, the compiler
has no way of knowing that it will not change the length and it will
call that function every time.

2.  Change the i++ in the for loop to ++i.  There are almost certainly
a few compilers out there that generate poorer code for the post
increment.

3.  The name of the function is very bad, the name says it counts
newlines, which it does not, instead it adds newlines.

But the big problem is that you are not inserting '\n' characters, you
are using them to overwrite characters that are already there.  If you
wanted to insert characters, you would need to know that the buffer
passed in with the source string had enough extra room to hold all of
the extra characters.

If you _know for sure_ that the passed in string has enough extra room
at the end, you might do something like this (warning, not compiled or
tested):

int addnewlines(char *string)
{
   int added = 0;
   int current = 0;
   int ch;
   char *dst = string;
   char *src = malloc(strlen(string) + 1);
   if (NULL == src)
   {
      /* handle memory error */
   }

   strcpy(src, string);

   while(*dst++ = (ch =*src++))
   {
      if ('\n' == ch)
      {
         current = 0;
      }
      else if (++current >= 97)
      {
         *(dst - 1) = '\n';
         *dst++ = ch;
         current = 0;
         ++added;
      }
   }

   strcpy(string, dst);
   free(src);
   return added;

Quote:
}

The reason for the two strcpy() calls is left as an exercise to the
reader.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq



Thu, 27 Jan 2005 15:45:13 GMT  
 string newline replace

Quote:

> >                      string[i]=='\n';

> The problem here is that this does not insert a newline,
> it overwrites a character of the string with a newline.
> The character that was originally there was lost.

Nothing is overwritten, and nothing is lost. Actually it compares string[i]
with a newline, and throws away the result of the comparison. <g>

--
Simon.



Fri, 28 Jan 2005 07:56:06 GMT  
 string newline replace

Quote:

> 1.  Don't call strlen() at each reinitialization of the loop.  Define
> a variable of type size_t and assign it by calling strlen() before the
> start of the loop.  Since your code modifies the string, the compiler
> has no way of knowing that it will not change the length and it will
> call that function every time.

 Actually I'ld go for

for(i=0; string[i] != '\0'; ++i) {

...

Quote:
}

there is no need for an extra variable...
--

"LISP  is worth learning for  the profound enlightenment  experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days."   -- Eric S. Raymond


Fri, 28 Jan 2005 21:24:46 GMT  
 string newline replace

Quote:
>           if(string[i]=='\n')

i've got a silly question:

according to the OS, the \n is defined by  CR + LF or just CR (or
something like that)....

how does C handles this? I mean, for C, \n can be considered as being
one char and you can do a comparison the way it is done here?

'jib



Sat, 29 Jan 2005 06:23:52 GMT  
 string newline replace

Quote:

>>           if(string[i]=='\n')

>i've got a silly question:

>according to the OS, the \n is defined by  CR + LF or just CR (or
>something like that)....

yes.

Quote:
>how does C handles this?

if a file is opened in text mode, then if reading in data, the
end-of-line markers (even if they're 2 or more actual bytes) are
converted to a '\n'. When writing out data, \n is converted to the
relevant output bytes.

Quote:
>I mean, for C, \n can be considered as being
>one char and you can do a comparison the way it is done here?

\n means "whatever this OS uses for \n".

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>



Sat, 29 Jan 2005 08:17:10 GMT  
 string newline replace

Quote:


> Zimmerman) wrote in comp.lang.c:

> > hi

> > I'm having trouble writing this function which takes a string and
> > counts the number of characters it takes from each newline. The count
> > will be zero when it sees a newline. If it reaches 97, it will insert
> > a newline. Here's what I have so far:

> > int countnewline(char *string)
> > {
> >       int i, ncounter;

> You need to initialize ncounter somewhere, either where you define it:

>    int i, ncounter = 0;

> >       for(i=0;i<strlen(string);i++){

> ...or in the initialization of the loop:

>     for (i=0, ncounter=0;i<strlen(string);i++)

> >           if(string[i]=='\n')
> >                 {
> >                      ncounter=0;
> >                 }
> >            else if(ncounter==97)
> >                 {
> >                      ncounter=0;
> >                      string[i]=='\n';

> The problem here is that this does not insert a newline, it overwrites
> a character of the string with a newline.  The character that was
> originally there was lost.

> >                 }
> >            else
> >                      ncounter++;
> > }

> A few more comments:

> 1.  Don't call strlen() at each reinitialization of the loop.  Define
> a variable of type size_t and assign it by calling strlen() before the
> start of the loop.  Since your code modifies the string, the compiler
> has no way of knowing that it will not change the length and it will
> call that function every time.

> 2.  Change the i++ in the for loop to ++i.  There are almost certainly
> a few compilers out there that generate poorer code for the post
> increment.

> 3.  The name of the function is very bad, the name says it counts
> newlines, which it does not, instead it adds newlines.

> But the big problem is that you are not inserting '\n' characters, you
> are using them to overwrite characters that are already there.  If you
> wanted to insert characters, you would need to know that the buffer
> passed in with the source string had enough extra room to hold all of
> the extra characters.

> If you _know for sure_ that the passed in string has enough extra room
> at the end, you might do something like this (warning, not compiled or
> tested):

> int addnewlines(char *string)
> {
>    int added = 0;
>    int current = 0;
>    int ch;
>    char *dst = string;
>    char *src = malloc(strlen(string) + 1);
>    if (NULL == src)
>    {
>       /* handle memory error */
>    }

>    strcpy(src, string);

>    while(*dst++ = (ch =*src++))
>    {
>       if ('\n' == ch)
>       {
>          current = 0;
>       }
>       else if (++current >= 97)
>       {
>          *(dst - 1) = '\n';
>          *dst++ = ch;
>          current = 0;
>          ++added;
>       }
>    }

>    strcpy(string, dst);
>    free(src);
>    return added;
> }

> The reason for the two strcpy() calls is left as an exercise to the
> reader.

i tried your suggestion Jack but there were a lot of errors on
compile. I'm pulling my hair on this one. I've haven't slept for 48
hours trying to write this function. Btw thanks to everyone for
replying. Here's my second entry:

void addnewlines(char string[])
{
    char newstring[4000], newstring2[4000], bufw[7000], temp[200];
    int i=0, tonewline=0, t=0, previpos=0, z=0;
    strcpy(newstring, string);
    strcpy(bufw, "");
    strcpy(newstring2, " ");
    for(;;i++)
    {
        if((newstring[i]=='\n')||(tonewline==97))
        {
                for(t=0;t<=i-previpos;t++,previpos++){
                      bufw[t]=newstring[previpos];
                }
                strcat(newstring2, bufw);
                strcpy(bufw, " ");
                tonewline=0;
        }
        else if(newstring[i]=='\0'){
                for(z=0;z<=i-previpos;z++, previpos++){
                      bufw[z]=newstring[previpos];}
                strcat(newstring2, bufw);  
                previpos=0;
                break;
        }
        else
                tonewline++;
    }
    strcpy(string, newstring2);
    strcat(string, "\n");

- Show quoted text -

Quote:
}



Sat, 29 Jan 2005 15:50:24 GMT  
 string newline replace

Quote:


> > (Bob Zimmerman) wrote in comp.lang.c:

> > > I'm having trouble writing this function which takes a string
> > > and counts the number of characters it takes from each newline.
> > > The count will be zero when it sees a newline. If it reaches
> > > 97, it will insert a newline. Here's what I have so far:

> > > int countnewline(char *string)
> > > {
> > >       int i, ncounter;

... snip ...

> i tried your suggestion Jack but there were a lot of errors on
> compile. I'm pulling my hair on this one. I've haven't slept for
> 48 hours trying to write this function. Btw thanks to everyone
> for replying. Here's my second entry:

> void addnewlines(char string[])
> {

UNTESTED suggestion follows:

#include <stdlib.h>
#include <string.h>

#define MAXPIECE 97

char *splitstring(const char * strng)
{
   int   lgh, ch, counter;
   char *result;  /* points to malloc'd memory */
   char *rp;      /* moving pointer */

   counter = 0;
   lgh = strlen(strng);
   /* at most the input line consists of all \n's */
   if ((result = calloc(2 * lgh + 1))) { /* zeroing it all */
      lgh = 0;
      rp = result;
      while ((ch = *strng)) {
         if ((MAXPIECE == ++lgh) && ('\n' != ch)) {  /* add nl */
            lgh = 0;
            rp++;
            counter++;
         }
         if ('\n' == ch) {         /* end line */
            rp++;
            lgh = 0;
            strng++;
            counter++;
         }
         else {                    /* move datum */
            *rp++ = ch;
            strng++;
         }
      } /* while */
      /* Here (rp - result) gives the length of result */
      return result;
  } /* splitstring - totally untested */

It is the callers responsibility to free the string(s) returned
from splitstring after use.  This will be NULL if the malloc
fails.  How to detect the end of the set of strings is another
matter.  Another int pointer parameter to receive counter might be
of use.

--

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



Sat, 29 Jan 2005 21:46:51 GMT  
 string newline replace

Quote:
> >how does C handles this?

> if a file is opened in text mode, then if reading in data, the
> end-of-line markers (even if they're 2 or more actual bytes) are
> converted to a '\n'. When writing out data, \n is converted to the
> relevant output bytes.

thanx a lot :)

'jib



Sun, 30 Jan 2005 00:01:30 GMT  
 string newline replace

Quote:


> Zimmerman) wrote in comp.lang.c:

> > hi

> > I'm having trouble writing this function which takes a string and
> > counts the number of characters it takes from each newline. The count
> > will be zero when it sees a newline. If it reaches 97, it will insert
> > a newline. Here's what I have so far:

> > int countnewline(char *string)
> > {
> >       int i, ncounter;

> You need to initialize ncounter somewhere, either where you define it:

>    int i, ncounter = 0;

> >       for(i=0;i<strlen(string);i++){

> ...or in the initialization of the loop:

>     for (i=0, ncounter=0;i<strlen(string);i++)

> >           if(string[i]=='\n')
> >                 {
> >                      ncounter=0;
> >                 }
> >            else if(ncounter==97)
> >                 {
> >                      ncounter=0;
> >                      string[i]=='\n';

> The problem here is that this does not insert a newline, it overwrites
> a character of the string with a newline.  The character that was
> originally there was lost.

> >                 }
> >            else
> >                      ncounter++;
> > }

> A few more comments:

> 1.  Don't call strlen() at each reinitialization of the loop.  Define
> a variable of type size_t and assign it by calling strlen() before the
> start of the loop.  Since your code modifies the string, the compiler
> has no way of knowing that it will not change the length and it will
> call that function every time.

> 2.  Change the i++ in the for loop to ++i.  There are almost certainly
> a few compilers out there that generate poorer code for the post
> increment.

> 3.  The name of the function is very bad, the name says it counts
> newlines, which it does not, instead it adds newlines.

> But the big problem is that you are not inserting '\n' characters, you
> are using them to overwrite characters that are already there.  If you
> wanted to insert characters, you would need to know that the buffer
> passed in with the source string had enough extra room to hold all of
> the extra characters.

> If you _know for sure_ that the passed in string has enough extra room
> at the end, you might do something like this (warning, not compiled or
> tested):

> int addnewlines(char *string)
> {
>    int added = 0;
>    int current = 0;
>    int ch;
>    char *dst = string;
>    char *src = malloc(strlen(string) + 1);
>    if (NULL == src)
>    {
>       /* handle memory error */
>    }

Is NULL==src is valid??I think it should be replaced by
src==NULL..Other wise it will give error for "lvalue"..

- Show quoted text -

Quote:
>    strcpy(src, string);

>    while(*dst++ = (ch =*src++))
>    {
>       if ('\n' == ch)
>       {
>          current = 0;
>       }
>       else if (++current >= 97)
>       {
>          *(dst - 1) = '\n';
>          *dst++ = ch;
>          current = 0;
>          ++added;
>       }
>    }

>    strcpy(string, dst);
>    free(src);
>    return added;
> }

> The reason for the two strcpy() calls is left as an exercise to the
> reader.



Sun, 30 Jan 2005 12:57:02 GMT  
 string newline replace

Quote:

> >    if (NULL == src)
> >    {
> >       /* handle memory error */
> >    }

> Is NULL==src is valid??I think it should be replaced by
> src==NULL..Other wise it will give error for "lvalue"..

NULL==src is valid, and means exactly the same as src==NULL. The equality
operator == takes the value of both sides and compares them. It does not
require an lvalue.

The main reason that people would write NULL==src is to make the compiler
complain if you make a mistake. If you accidentally wrote NULL=src, the
compiler must complain. If you instead write src=NULL by mistake, the
compiler need not complain, and then you have a bug which is harder to
find.

--
Simon.



Sun, 30 Jan 2005 15:03:00 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Replace more then one charaster using String.Replace()

2. replacing string in string

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

4. insert newlines into long string

5. length of strings w. newlines in them

6. stripping newlines from strings

7. newlines in string constants

8. building a string with newline characters

9. newline in string

10. Adding a newline to a string

11. Newline in commandline string

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

 

 
Powered by phpBB® Forum Software