下面代码为什么会提示冲突int replace(char*str, char c1, char c2) {int count = 0;int i = 0;while (*str != '\0') {if (str[i] = c1){str[i] = c2;count++;}i++;}return count;}int main(){char A[5] = "abcd";cout << "count:" << replace(A, 'a', 'f') << endl << "A" << A;return 0;}
2 回答
红糖糍粑
TA贡献1815条经验 获得超6个赞
1、星str表示char数组的第一个元素,所以永远不可能=\0,while死循环,i一直自增,str[i]越界
2、str[i] = c1;是赋值,str[i] = =c1才是判断
芜湖不芜
TA贡献1796条经验 获得超7个赞
来,给你改了一下,运行试试:
int replace(char*str, char c1, char c2) { int count = 0; int i = 0; while (str[i] != '\0') { if (str[i] == c1){ str[i] = c2; count++; } i++; } return count; } int main() { char A[5] = "abcd"; cout << "count:" << replace(A, 'a', 'f') << endl << "A:" << A; return 0; }
- 2 回答
- 0 关注
- 683 浏览
添加回答
举报
0/150
提交
取消