Originally Posted by
paperbox005
does anyone know how i can use sscanf to replace characters in a string?
First, I wouldn't use sscanf.
Originally Posted by
paperbox005
say i have string
abcd
and i want to replace a and d with a space, how coul di do that?
I'd just loop through the source string looking for the character to find and replace it with the desired string.
Code:
#include <stdio.h>
#include <string.h>
char *foo(char *dst, const char *src, char find, const char *replace)
{
char *start = dst;
while ( *src )
{
if ( *src == find )
{
strcpy(dst, replace);
dst += strlen(replace);
}
else
{
*dst++ = *src;
}
++src;
}
*dst = '\0';
return start;
}
int main()
{
static const char before[] = "abcdabcdabcd";
char after [ sizeof before * 2 ];
puts(foo(after, before, 'a', "d "));
return 0;
}
/* my output
d bcdd bcdd bcd
*/