2012-10-15 16 views
4

可能重复:
What is the function to replace string in C?字符串中的字符替换

我想在我的字符串与多个字符替换某个字符。这是我正在尝试做的一个例子。

说我有字符串“aaabaa”

我想替换字母“b”,5个“C”的所有事件。

所以,当我完成后, “aaabaa” 变成 “aaacccccaa”

我写了下面的代码:

#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); 
} 

我从这个函数输出为 “aaaccccc”。看来它只是用c's覆盖最后两个a。我有没有办法让这些最后几个不会被覆盖?

+0

你需要['的memmove()'](http://www.cplusplus.com/reference/clibrary/cstring/memmove/) –

+1

只是让了一下假设*字符串中的每个*字符都是'b'...您的目标缓冲区最好比调用时占用它的字符串长5倍。至少如果你想这样做* in-place *你*需要*编码算法来说明正在进行替换的内容占用的* buffer *的大小(不仅仅是字符串)。不这样做最终是一场灾难。 – WhozCraig

回答

5

你的问题是,你替换“CCCCC”到原始字符串这样你的愿望,以取代后覆盖剩余的字符。 ..你应该复制到一个新的字符串并跟踪两个索引 - 每个索引。

,很快乐,你宣称比你原来的字符串的大小char s[20]大加替换值,否则你必须在你的关键的登录系统创建:-)

干杯一个缓冲区溢出漏洞,

-1

使用此功能:

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; 
    } 

为您的情况:printf("%s\n", replace(s,"b","ccccc"));

+0

这并不是每个实例,但是...因为您正在使用静态缓冲区,所以这非常脆弱。 – nneonneo

+0

...并祈祷* st不会引用字符串4097个字符或更长,并以'b'结尾。 – WhozCraig

0

那么,如果你要动态分配数组,你可能必须分配第二个数组。这是必要的,因为你的字符串只有固定数量的内存分配。

所以,我不建议tryig来覆盖for循环中的字符,而是建议增加一个计数器,告诉你新的数组有多大。您的计数器应该以您原始字符串的大小开始,并在每次找到'b'的实例时增加4。然后,您应该能够编写一个函数,将修改后的字符串适当地复制到一个新的字符缓冲区,大小为[counter],每找到一个'b'就插入5个c。

8

如果你想这样做,在一般情况下,无需担心试图大小的缓冲区,你应该malloc一个新字符串只是大到足以容纳结果:

/* 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; 
} 

用法:

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

有没有'const char * s'而是'char * s'? – dforce

1

有必要声明第二个字符数组。在下面的代码中,它只是在条件失败时将数组s的内容复制到s1。

#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

您可以使用不同的变量

#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
#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". 
} 

在这里我们使用的是缓冲(TEMP)的字符串的其余部分保存我们要替换字符之后。替换完成后,我们将它追加到最后。

所以我们得到S = “aaacccccaa”