help me with strings

Richard A. O'Keefe ok at goanna.cs.rmit.oz.au
Tue May 21 10:55:41 AEST 1991


In article <CSHORT.91May20160106 at haywire.crl>, cshort at 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.



More information about the Comp.lang.c mailing list