re: OFF-TOPIC C: delete_doubledollar

From: Darrin Wilson (wilson@shark.cc.cc.ca.us)
Date: 07/07/96


	Well, Im board.. ;)

> 
> char *delete_doubledollar(char *string)
> {
>   char *read, *write;
> 
>   if ((write = strchr(string, '$')) == NULL)
>     return string;

	Point 'write' to the first occurance of a '$' in our string, if
there isn't one, then just return the original string.

> 
>   read = write;

	(obviously) make read point to write which points to the first '$'
in our string. If there is a $$ then read and write will soon be NOT
pointing at the same location in memory.

> 
>   while (*read)

	While *read is greater than a 0 (or '\0' end of string) ...

>     if ((*(write++) = *(read++)) == '$')
>       if (*read == '$')
>         read++;

	This is prolly where you get lost. What we are doing here is copying
what ever character that [read] is pointing at into the location that [write]
is pointing to, but if the character that [read] is pointing to is a '$', then
move [read] to the next character, in affect ignoring it rather than write it
back out... also, after we evaulate what is in [read] and [write], increment
them to the next location in the string, until we hit the end of the string
which should be terminated with a '\0' character (we hope, and 99.9% of the
time it is).

> 
>   *write = '\0';

	Terminate the string...

> 
>   return string;
> }
 
	And return the results...

  One more time in case we lost someone:

	We are accessing the string pointed to by 'string' and overwritting
what is there with a the same thing except we are ignoring the second '$'
characters. This is an easy way to remove the extra '$' characters without
having to bother allocating any temporary space, not that we need it when
we are removeing things from a string, just remember that we need to put
that terminator at the end, otherwise we would end up with the last char's
of the string duplicated at the end.

[This is a test string with $$ in it.]

  Without a '\0' at the end our string would look like this:

[This is a test string with $ in it..]

	If you wanted to take a string and, for ever $ put a $$ you would
then need to allocate a temporary chunk of memory larger than the one we
started with, perhaps (strlen(string) * 2) just in case. I wrote a func
called 'replace_string()' for the Mud that Im helping code on, and if
anyone would like it, I can post it... for that matter, I've wrote a number
of helper func's like that now. :)

	Hope that explains that (in a long winded manner)... as I said,
Im bored. ;)

-Darrin



This archive was generated by hypermail 2b30 : 12/07/00 PST