I only have access to \'C\' and need to replace characters within a character array. I have not come up with any clean solutions for this relatively simple procedure.
char *s = (char*)strBuffer;
char sClean[strlen(strBuffer) + 1]; /* +1 for null-byte */
/* if above does not work in your compiler, use:
char *sClean = (char*)malloc(sizeof(strBuffer) + 1);
*/
int i=0;
while (*s)
{
sClean[i++]= *s;
if ((*s == '&') && (!strncmp(s, "&", 5)) s += 5;
else s++;
}
sClean[i] = 0;
Allocate another buffer, either on the stack or the heap, and then copy the string into the new buffer 1 character at a time. Make special handling when you encounter the &
character.
Basically, you need to:
C isn't noted for it's ease of use, especially when it comes to strings, but it has some rather nice standard library functions that will get the job done. If you need to work extensively on strings you'll probably need to know about pointers and pointer arithmetic, but otherwise here are some library functions that will undoubtedly help you:
'&'
) in a string.strchr()
/strcmp()
combination).