替换字符

基本类型 / 字符串

//a string literal is read-only, an array made from it is not
char str[] = "1-3-2";
str[2] = '2';
str[4] = '3';
//str is "1-2-3"
printf("str is %s\n", str);

//replace every '-' with '+'
for (char *p = strchr(str, '-'); p != nullptr; p = strchr(p + 1, '-')) {
    *p = '+';
}
//str is "1+2+3"
printf("str is %s\n", str);