"Arthur J. O'Dwyer" wrote:
>
> On Wed, 7 Apr 2004, Toto wrote:
> >
> > I have the following string:
> > "table+camera"
> > and I want to remove the + sign:
> > "tablecamera".
> >
> > How do i do that ?
>
> #include <string.h>
>
> char s[] = "table+camera";
> char *p;
>
> while ((p = strchr(s,'+')) != NULL)
> strcpy(p, p+1);
This is only guaranteed to work if the '+' is the
last character in the string. Otherwise the source and
destination overlap, which is a no-no for strcpy(). A
safe alternative is
memmove(p, p+1, strlen(p+1)+1);
.... which can be simplified to
memmove(p, p+1, strlen(p));
Also, the Little Tin God might be better propitiated
by replacing the `while' with
p = s;
while ((p = strchr(p, '+')) != NULL)
.... thus not searching and re-searching and re-re-searching
parts of `s' already known to be free of '+' characters.
--