Hi:
I've had this in my library forever. You might not like the occurrence flag:
Regards,
Ed
#include <stdio.h>
void replace();
void main()
{
char *str = ",a string, of tokens,";
printf("%s\n", str);
replace(str, '&', ',', 0);
printf("%s\n", str);
}
/*
* This function replaces old_char with new_char in string.
* If the convert flag is 0 replace all occurrences of old char.
* If the flag > 0, only replace that occurrence of the character
*/
void replace(string, new_char, old_char, convert_flag)
char *string;
char new_char;
char old_char;
int convert_flag;
{
int c_flag=0;
while(*string)
{
if(*string == old_char)
c_flag++;
if(convert_flag == 0 || convert_flag == c_flag)
if(*string == old_char)
{
*string = new_char;
if(convert_flag > 0)
break;
}
string++;
}
}