【发布时间】:2021-01-14 00:17:25
【问题描述】:
我正在尝试反转一个字符串,但它保持不变。除了<string.h> 和<stdio.h>,我不使用任何模块。
void rev(s){
char i, temp;
char *sf = s;
char ri = strlen((s) - 1);
char *sl = &s[ri];
for (i = 0; i < ri; i++){
if (*sf != *sl){
temp = *sf++;
s[i] = *sl--; //
s[ri--] = temp; //those two seems to be getting new characters, but it won't
}
else {
ri--;
sf++;
sl--;
}
}
printf("%s", s);
}
【问题讨论】:
-
你试过用笔和纸运行你的程序吗?
-
@Deno 函数不能编译。
-
char ri = strlen((s1) - 1);是做什么的?没有名为s1的变量。 -
不要反转整个字符串,反转一半。
-
strlen((s) - 1)当然应该读作strlen(s) - 1
标签: c char reverse c-strings function-definition