- 论坛徽章:
- 0
|
把字符串中的‘:’转换成'\0'。大家帮忙调一下
void colon_conversion1(char *buf) {
char *p;
int colons = 0;
p = buf;
while ((p != NULL) && (colons < 5)) {
if (*p == ':') {
*p = '\0';
colons++;
}
p++;
}
}
void colon_conversion2(char *buf) {
int colons = 0;
while ((*buf != '\0') && (colons < 5)) {
if (*buf == ':') {
*buf = '\0'; colons++;
}
buf++;
}
}
int main()
{
char *str = "h:el:lo :wo:rd:!";
fputs(str,stdout);
fputs("\n",stdout);
colon_conversion1(str);
colon_conversion1(str);
fputs(str,stdout);
fputs("\n",stdout);
return 0;
}
分别调用两个函数打印的结果都是
[root@localhost testm]# ./ces
h:el:lo :wo:rd:!
段错误 (core dumped) |
|