Replacing a character in a string

Possible duplicate:
What is a function to replace a string in C?

I am trying to replace some character in my string with several characters. Here is an example of what I'm trying to do.

Say I have the line "aaabaa"

I want to replace all occurrences of the character "b" with 5 "c" s.

So when I finished, "aaabaa" will become "aaacccccaa"

I wrote the following code:

#include <stdio.h> #include <string.h> int main(void) { char s[20] = "aaabaa"; int i, j; for (i=0; s[i]!= '\0'; i++) { if (s[i] == 'b') { for (j=0; j<5; j++) { s[i+j] = 'c'; } } } printf("%s\n", s); } 

My conclusion to this function is "aaaccccc". It seems like he just overwrites the last two a with c. Is there any way that I would do this so that these last pairs are not overwritten?

+4
source share
7 answers

Your problem is that you replace "ccccc" with the original line, thereby overwriting the remaining characters after what you want to replace ... You must copy to a new line and track two indexes - one in each.

And be glad that you declared char s[20] larger than the size of your source string plus replacement values, because otherwise you would create a buffer overflow vulnerability in your critical login system :-)

Greetings

+5
source

If you want to do this in general, without worrying about trying to determine the size of your buffers, you must malloc create a new line large enough to hold the result:

 /* return a new string with every instance of ch replaced by repl */ char *replace(const char *s, char ch, const char *repl) { int count = 0; const char *t; for(t=s; *t; t++) count += (*t == ch); size_t rlen = strlen(repl); char *res = malloc(strlen(s) + (rlen-1)*count + 1); char *ptr = res; for(t=s; *t; t++) { if(*t == ch) { memcpy(ptr, repl, rlen); ptr += rlen; } else { *ptr++ = *t; } } *ptr = 0; return res; } 

Using:

 int main() { char *s = replace("aaabaa", 'b', "ccccc"); printf("%s\n", s); free(s); return 0; } 
+8
source

You must declare a second char array. In the code below, it just copies the contents of the array s to s1 when the condition fails.

 #include <stdio.h> #include <string.h> int main(void) { char s[20] = "aaabaa"; char s1[1024]; int i, j, n; for (i=0, n = 0; s[i]!= '\0'; i++) { if (s[i] == 'b') { for (j=0; j<5; j++) { s1[n] = 'c'; n++; } } else { s1[n] = s[i]; n++; } } s1[n] = '\0'; printf("%s\n", s1); } 
+1
source

You can use another variable

 #include <stdio.h> #include <string.h> int main(void) { char s[20] = "aaabaa"; char temp[20]=""; int i, j,k; k=0; for (i=0; s[i]!= '\0'; i++) { if (s[i] == 'b') { for (j=0; j<5; j++) { temp[k] = 'c'; k++; } } else { temp[k]=s[i]; k++ } } printf("%s\n", temp); } 
+1
source
 #include <stdio.h> #include <string.h> int main(void) { char temp[20]; char s[20] = "aaabaa"; int i, j; for (i=0; s[i]!= '\0'; i++) { if (s[i] == 'b') { strcpy(temp,s[i+1]); //copy rest of the string in this case 'aa' for (j=0; j<5; j++) { s[i+j] = 'c'; } s[i+j] = '\0'; // here we get s = "aaaccccc" strcat(s,temp); // concat rest of the string (temp = "aa") after job is done. // to this point s becomes s = "aaacccccaa" } } printf("%s\n", s); //s = "aaacccccaa". } 

here we use a buffer (temp) to hold the rest of the line after our replacement character. after the replacement is completed, we will add it to the end.

therefore we get s = "aaacccccaa"

+1
source

Well, if you plan on dynamically allocating an array, you probably have to allocate a second array. This is necessary because your string s contains only a fixed amount of allocated memory.

So, instead of tryig, to rewrite the characters in the for loop, I would suggest increasing the counter, which told you how big your new array is. Your counter should begin with the size of the original row and increment by 4 each time an instance of "b" is found. Then you should be able to write a function that appropriately copies the modified string to a new char buffer of size [counter], inserting 5 c each time "b" is found.

0
source

Use this function:

 char *replace(char *st, char *orig, char *repl) { static char buffer[4096]; char *ch; if (!(ch = strstr(st, orig))) return st; strncpy(buffer, st, ch-st); buffer[ch-st] = 0; sprintf(buffer+(ch-st), "%s%s", repl, ch+strlen(orig)); return buffer; } 

for your case: printf("%s\n", replace(s,"b","ccccc"));

-1
source

Source: https://habr.com/ru/post/1439740/


All Articles