Yes, you can do it this way, but adds a little more overhead since your
doing more commands.

> char *new_smash_tilde (const char *str)
> {
>   static char newbuf[MSL];
>   int count;
>
>   for (count = 0 ; *str != '\0'; str++, count++)
>   {
>      if (*str == '~')
>      {
>        newbuf[count] = '&';
>        count++;
>        newbuf[count] = '-';
>      }
>      else
>        newbuf[count] = *str;
>   }
>
>   count++;
>   newbuf[count] = '\0';
>
>   return newbuf;
> }
>
> Of course, you'd need to call something like
>    string = new_smash_tilde(string);
>
> instead of the way the original is called.
>
> If there is an easier/more elegant way to do this I'm all ears :).
>
> Also, quick question.  In the loop, is it possible to just do a
>    newbuf[++count] = '-';
> Instead of incrementing the counter on a seperate line?  I seem to
> recall seeing this done somewhere but it returned an error.


Yes, you can do it this way as well. However, you should do newbuf[count++],
since you want to increment the counter "after" you've put data in that
spot, not before.
Remember, if count = 10, newbuf[++count] position will become newbuf[11],
whereas newbuf[count++] will be newbuf[10] and then increment "after" its
done with the command. Depending on where you have the ++ will determine
weather the increment happens "before" or "after" the command.
When on a separate line, you can do ++count or count++, because it's all by
it's lonesome self and doesn't matter. But does matter when it's within []'s
of an array.

Happy coding,
Rheede


Reply via email to