[comp.lang.c] help me with strings

cshort@haywire.crl (Spmg*d, Lord of Potted Meat Product) (05/21/91)

c novice here

i have a large string about 9k and there are some
characters i need to strip out '^M', i'm not sure
how i can do that. i was thinking copy the sting
into an array then just scan for ^M and replace with
spaces. unfortunitly i don't speak enough c to do this.

the string is called dna, and the array is called dnary
any ideas.

thanks

cshort@nmsu.edu

--

 |----------------------|----------------------                     | 
 |the world is spam and |    Chris Short                            |
 | we are but the key   |          Computing Research Labatory      |
 |--------|-------------|          Box 30001/3 CRL                  |      
          |                        New Mexico State University      |
          |                        Las Cruces, NM 88003-0001        | 
          |      Email: Cshort@nmsu.edu  Fax:505 646 6218|-------------------|
          |      Voice: 505 646 6216                     |SpamKey productions| 
          |                     -------------------------|-------------------|

ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) (05/21/91)

In article <CSHORT.91May20160106@haywire.crl>, cshort@haywire.crl (Spmg*d, Lord of Potted Meat Product) writes:
> C novice here.
> 
> I have a large string about 9k and there are some
> characters I need to strip out '^M'.  I'm not sure
> how I can do that. I was thinking copy the string
> into an array then just scan for ^M and replace with
> spaces.  Unfortunately I don't speak enough C to do this.
> 
> The string is called dna, and the array is called dnary
> Any ideas.

/* If you really want to replace Carriage Returns by Spaces */

/*  strcpy_replacing(dst, src, old, new)
    copies src to dst, as strcpy(dst, src) would, except that
    "old" characters are replaced by "new" ones.
*/
void strcpy_replacing(char *dst, char *src, int old, int new)
    {
	int c;

	while (c = *src++)
	    *dst++ = (c == old ? new : c);
	*dst = '\0';
    }

/*  memcpy_replacing(dst, src, len, old, new)
    copies len bytes from src to dst, as memcpy(dst, src, len) would,
    except that "old" bytes are replaced by "new" ones.
    Use this when you know the length of src and don't want len taken
    as a terminator.
*/
void memcpy_replacing(char *dst, char *src, size_t len, int old, int new)
    {
	int c;

	while (len-- != 0) {
	    c = *src++;
	    *dst++ = (c == old ? new : c);
	}
    }

/*  However, it may be that your 9k "string" is the contents of a file,
    and that what you really have is CR LF (^M^J) pairs, and you just
    want to get rid of the CRs (^Ms).  In that case you want to throw
    your "old" characters away.
*/

/*  strcpy_deleting(dst, src, old)
    copies src to dst, as strcpy(dst, src) would, except that "old"
    characters are not copied at all.  It returns the length of dst.
*/
int strcpy_deleting(char *dst, char *src, int old)
    {
	char *d;
	int c;

	for (d = dst; c = *src++; )
	    if (c != old) *d++ = c;
	*d = '\0';
	return d-dst;
    }

/*  memcpy_deleting(dst, src, len, old)
    copies len bytes from src to dst, as memcpy(dst, src, len) would,
    except that "old" bytes are not copied at all.  It returns the
    number of bytes that were copied, the new effective length of dst.
*/
int memcpy_deleting(char *dst, char *src, size_t len, int old)
    {
	char *d;
	int c;

	for (d = dst; len-- != 0; )
	    if ((c = *src++) != old) *d++ = c;
	return d-dst;
    }

So the answer to the question may be

	strcpy_replacing(dnary, dna, '\r', ' ');
or	strcpy_deleting(dnary, dna, '\r');
or	dnary_len = memcpy_replacing(dnary, dna, dna_len, '\r', ' ');
or	dnary_len = memcpy_deleting(dnary, dna, dna_len, '\r');
depending on exactly what you have and/or want.

A more general way of doing it is to look at the UNIX 'tr' command
and to implement something similar in C.  Indeed, if your "string"
is coming from a file, the best way to get rid of the Carriage Returns
might be to make a cleaned-up copy of the file using 'tr'.
If the file is coming from another computer, you should investigate
whether your file transfer tool will translate between various line
terminator conventions for you (most will).

-- 
There is no such thing as a balanced ecology; ecosystems are chaotic.