Remove carriage and line feeds from a string 
Author Message
 Remove carriage and line feeds from a string

I am trying to remove carriage and line feeds from a string.
Anyone know of a simple way to find them, replace them, or
strip them out?

Keith

Keith Cotroneo

Keith Cotroneo



Fri, 23 Aug 2002 03:00:00 GMT  
 Remove carriage and line feeds from a string
/* returns -1 if empty string, else returns length */

int RemoveControlChars( char *ptr )
{
    int  Result;
    Result = 0;
    if( ptr != NULL )
    {
        while( *ptr != '\0' )
        {
            if(( *ptr == '\r' )      /* return = 0x0D (ASCII) */
              ||( *ptr == '\n' ))    /* linefeed = 0x0A (ASCII) */
            {
                *ptr = ' ';  /* substitute a space character */
            }
            ptr++;  /* move on to next character */
            Result++;
        }
    }
    else Result--;
    return Result;

Quote:
}

If you want to strip them, you need a target buffer to
copy the non-control characters to. Then you copy
only those you wish to keep and copy the buffer back
to the original on completion. (Note I only coded for
the ones you mentioned, you might want to make the
test as ( *ptr < ' ' ) to test for "before the space character
in ASCII", which is where the ASCII control chars exist.
The <ctype.h> has "iscntrl(...)" defined to determine
basically the same thing. The code sample can be
reduced significantly....if you don't want any status indicator.

void ReMapControlChars( char *ptr )
{
    while(( ptr != NULL )&&( *ptr != '\0' ))
    {
        if(( *ptr == '\r' )||( *ptr == '\n' ))
            *ptr = ' ';
        ptr++;
    }

Quote:
}

-------------------------------------------------------------------
"If you think you know the answer,
then you don't understand the question !"
-------------------------------------------------------------------


Quote:
> I am trying to remove carriage and line feeds from a string.
> Anyone know of a simple way to find them, replace them, or
> strip them out?

> Keith

> Keith Cotroneo

> Keith Cotroneo




Fri, 23 Aug 2002 03:00:00 GMT  
 Remove carriage and line feeds from a string

Quote:
>/* returns -1 if empty string, else returns length */

>int RemoveControlChars( char *ptr )
>{
>    int  Result;
>    Result = 0;
>    if( ptr != NULL )
>    {
>        while( *ptr != '\0' )
>        {
>            if(( *ptr == '\r' )      /* return = 0x0D (ASCII) */
>              ||( *ptr == '\n' ))    /* linefeed = 0x0A (ASCII) */
>            {
>                *ptr = ' ';  /* substitute a space character */
>            }
>            ptr++;  /* move on to next character */
>            Result++;
>        }
>    }
>    else Result--;
>    return Result;
>}

>If you want to strip them, you need a target buffer to
>copy the non-control characters to. Then you copy
>only those you wish to keep and copy the buffer back
>to the original on completion. (Note I only coded for
>the ones you mentioned, you might want to make the
>test as ( *ptr < ' ' ) to test for "before the space character
>in ASCII", which is where the ASCII control chars exist.
>The <ctype.h> has "iscntrl(...)" defined to determine
>basically the same thing. The code sample can be
>reduced significantly....if you don't want any status indicator.

>void ReMapControlChars( char *ptr )
>{
>    while(( ptr != NULL )&&( *ptr != '\0' ))
>    {
>        if(( *ptr == '\r' )||( *ptr == '\n' ))
>            *ptr = ' ';
>        ptr++;
>    }
>}

>-------------------------------------------------------------------
>"If you think you know the answer,
>then you don't understand the question !"
>-------------------------------------------------------------------



>> I am trying to remove carriage and line feeds from a string.
>> Anyone know of a simple way to find them, replace them, or
>> strip them out?

>> Keith

>> Keith Cotroneo

>> Keith Cotroneo


Wonderful, thank you. I get the idea now.

Keith

Keith Cotroneo



Fri, 23 Aug 2002 03:00:00 GMT  
 Remove carriage and line feeds from a string


Quote:
>I am trying to remove carriage and line feeds from a string.
>Anyone know of a simple way to find them, replace them, or
>strip them out?

Search with strchr() from <string.h>. When found, kill it (replace by 0).

--
-hs-
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC



Fri, 23 Aug 2002 03:00:00 GMT  
 Remove carriage and line feeds from a string


Quote:


>>I am trying to remove carriage and line feeds from a string.
>>Anyone know of a simple way to find them, replace them, or
>>strip them out?

>Search with strchr() from <string.h>. When found, kill it (replace by 0).

So if I have the following string:

"First line\nSecond line"

I should replace the '\n' with '\0'?

"First line"

Doesn't seem right to me.... ;-)

--
Chris Mears


ICQ: 36697123

C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html



Fri, 23 Aug 2002 03:00:00 GMT  
 Remove carriage and line feeds from a string
Chris Mears a crit dans le message ...
Quote:




>>>I am trying to remove carriage and line feeds from a string.
>>>Anyone know of a simple way to find them, replace them, or
>>>strip them out?

>>Search with strchr() from <string.h>. When found, kill it (replace by 0).

>So if I have the following string:

>"First line\nSecond line"

>I should replace the '\n' with '\0'?

>"First line"

>Doesn't seem right to me.... ;-)

Ok ok, you're right, but according to the OP, it could work... in some
cases.
But the O.P. was not very clear about what to do once found.

Does
"First line\nSecond line"
give

"First lineSecond line"
or
"First line Second line"
or
"First line"
"Second line"
or ...
--
-hs-
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC



Fri, 23 Aug 2002 03:00:00 GMT  
 Remove carriage and line feeds from a string


Quote:
>/* returns -1 if empty string, else returns length */

>int RemoveControlChars( char *ptr )
>{
>    int  Result;
>    Result = 0;
>    if( ptr != NULL )
>    {
>        while( *ptr != '\0' )
>        {
>            if(( *ptr == '\r' )      /* return = 0x0D (ASCII) */
>              ||( *ptr == '\n' ))    /* linefeed = 0x0A (ASCII) */
>            {
>                *ptr = ' ';  /* substitute a space character */
>            }
>            ptr++;  /* move on to next character */
>            Result++;
>        }
>    }
>    else Result--;
>    return Result;
>}

>If you want to strip them, you need a target buffer to
>copy the non-control characters to. Then you copy
>only those you wish to keep and copy the buffer back
>to the original on completion.

Not necessarily.  I haven't thought about relative efficiency, but one could
easily delete characters "in place" by using memmove() to copy the trailing
part of the string, each time a '\r' or '\n' is encountered.
--



Fri, 23 Aug 2002 03:00:00 GMT  
 Remove carriage and line feeds from a string


<snip>
Quote:
> >If you want to strip them, you need a target buffer to
> >copy the non-control characters to. Then you copy
> >only those you wish to keep and copy the buffer back
> >to the original on completion.

> Not necessarily.  I haven't thought about relative efficiency, but one
could
> easily delete characters "in place" by using memmove() to copy the
trailing
> part of the string, each time a '\r' or '\n' is encountered.
> --

<snip>

A relatively simple way is as follows (I don't like using
library functions if the problem is simple enough, embedded
toys rarely have the space to spare...)

void ReMapControlChars( char *pRead )
{
    char  *pWrite;

    pWrite = pRead;
    while(( pRead != NULL )&&( *pRead != '\0' ))
    {
        if(( *pRead == '\r' )||( *pRead == '\n' )) pRead++;  /* skip */
       else  *pWrite++ = *pRead++;                                  /* copy
and cont. */
    }
    pWrite = '\0';   /* force termination of result */

Quote:
}

Performs the removal in situ. You would probably find
that it optimises well enough with most compilers that
not using the library function memmove(...) (which is
commonly written in assembly) will not be any great
impediment to performance since you can't assume
that the string only contains a single '\n' or '\r'..... The
above only assumes that pRead is pointing at writable
memory....


Sat, 24 Aug 2002 03:00:00 GMT  
 Remove carriage and line feeds from a string


Quote:
>Chris Mears a crit dans le message ...




>>>>I am trying to remove carriage and line feeds from a string.
>>>>Anyone know of a simple way to find them, replace them, or
>>>>strip them out?

>>>Search with strchr() from <string.h>. When found, kill it (replace by 0).

>>So if I have the following string:

>>"First line\nSecond line"

>>I should replace the '\n' with '\0'?

>>"First line"

>>Doesn't seem right to me.... ;-)

>Ok ok, you're right, but according to the OP, it could work... in some
>cases.
>But the O.P. was not very clear about what to do once found.

>Does
>"First line\nSecond line"
>give

I'd say either this one:

Quote:
>"First lineSecond line"

or this one:

Quote:
>"First line Second line"

Both ways, it's a fairly trivial problem to be solved using strchr().
The first one is slightly harder, but not mind-bogglingly so.

--
Chris Mears


ICQ: 36697123

C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html



Sat, 24 Aug 2002 03:00:00 GMT  
 Remove carriage and line feeds from a string
On Mon, 6 Mar 2000 14:53:42 +1000, "Zorallin"

Quote:

>/* returns -1 if empty string, else returns length */

>int RemoveControlChars( char *ptr )
>{
>    int  Result;
>    Result = 0;
>    if( ptr != NULL )
>    {
>        while( *ptr != '\0' )
>        {
>            if(( *ptr == '\r' )      /* return = 0x0D (ASCII) */
>              ||( *ptr == '\n' ))    /* linefeed = 0x0A (ASCII) */
>            {
>                *ptr = ' ';  /* substitute a space character */
>            }
>            ptr++;  /* move on to next character */
>            Result++;
>        }
>    }
>    else Result--;
>    return Result;
>}

>If you want to strip them, you need a target buffer to
>copy the non-control characters to. Then you copy
>only those you wish to keep and copy the buffer back
>to the original on completion.

   Actually, since the stripped string will never be longer than the
original string, you can use the string itself as a buffer. Like this:

/* I haven't attempted to compile this code. However, if any     */
/* errors are found, you get double your money back.             */

#include <ctype.h>

/* If NO_STRIP is defined, it will replace any number of '\r'    */
/* and/or '\n' by a single whitespace, unless they're preceded   */
/* by at least one space, in which case they will be completely  */
/* ignored. If NO_STRIP is _not_ defined (e.g. by commenting out */
/* the line below, all and every '\r' and '\n' are stripped from */
/* the input string. All modifications are done in place.        */

#define NO_STRIP

int RemoveControlChars( char *ptr )
{
    int  Result;
    char* inp = ptr;
    Result = 0;
    if( inp != NULL )
    {
        while( *inp != '\0' )
        {
            if(( *inp == '\r' )      /* return = 0x0D (ASCII) */
              ||( *inp == '\n' ))    /* linefeed = 0x0A (ASCII) */
            {
#ifdef NO_STRIP
                /* If last char. is not already a space, put one */
                /* It checks Result to avoid accessing memory    */
                /* before the string.                            */
                if(!Result || !isspace(*(ptr-1)))
                {
                    *ptr = ' ';  /* substitute a space character */
                }
                else
                {
#endif
                    ptr++;
                    continue;
#ifdef NO_STRIP
                }
#endif
            }
            else
            {
                *ptr = *inp;
            }
            inp++;  /* move on to next character */
            ptr++;
            Result++;
        }
    }
    else Result--;
    return Result;

Quote:
}

Regards,
Diego Berge.


Sat, 24 Aug 2002 03:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Carriage return / line feed?

2. Carriage return/line feed in CEdit

3. Carriage and Line Feed characters?

4. Command line parameters with line feeds???

5. How to do a line feed in a multi-lined TextBox

6. printing line feed character???

7. Line feeds and tabs in an edit control

8. ? line feeds in CStatics

9. Feeding Lines in an Edit Box

10. Line feeds and tabs in an edit control

11. print just one line ( no form feed )

12. Insert a line feed in a edit control

 

 
Powered by phpBB® Forum Software