小编Rai*_*orn的帖子

如何用一个字符替换两个字符?

所以我试图cc&一个句子替换(例如:)"Hi this is Mark cc Alice"。到目前为止,我有这个代码,它替换了第一个c&但第二个c仍然存在。我将如何摆脱第二个c

int main() {
    char str[] = "Hi this is Mark cc Alice";
    int i = 0;
    while (str[i] != '\0') {
        if (str[i] == 'c' && str[i + 1] == 'c') {
            str[i] = '&';  
            //str[i + 1] = ' ';
        }
        i++;
    }
    printf("\n-------------------------------------");
    printf("\nString After Replacing 'cc' by '&'");
    printf("\n-------------------------------------\n");
    printf("%s\n",str);
    return str;
}
Run Code Online (Sandbox Code Playgroud)

输入是:

Hi this is …
Run Code Online (Sandbox Code Playgroud)

c loops c-strings char

6
推荐指数
1
解决办法
96
查看次数

标签 统计

c ×1

c-strings ×1

char ×1

loops ×1