For example, I choose a string "sdoeoeeoasd". I should replace all the 'o' with 'e' and vice versa: "sdeoeooeasd". I intend to do this with the string
lib.
I found a pretty similar question (Standard function to replace character or substring in a char array?), but I don't get how to make the code work according to my condition. It happens that the first occurence of the character is replaced, and then the only one character replaces:
char* replace_char(char* str, char find, char replace){
char temp = find;
char *current_pos = strchr(str,find);
char *current_posc2 = strchr(str,replace);
while (current_pos) {
*current_pos = replace;
current_pos = strchr(current_pos,find);
}
while (current_posc2) {
*current_posc2 = find;
current_posc2 = strchr(current_posc2, replace);
}
return str;
}
With c1='e'
and c2='o'
I get:
I have a thought about adding the third temp
variable, but my suggestions of its implementation were wrong and didn't work.