我已经写了一个程序来翻转字符串。但是它没有起作用。它输出的是与输入相同的字符串。代码有什么问题呢?
#include <stdio.h>
#include <stdlib.h>
char *strrev(char *s)
{
char *temp = s;
char *result = s;
char t;
int l = 0, i;
while (*temp) {
l++;
temp++;
}
temp--;
for (i = 0; i < l; i++) {
t = *temp;
*temp = *s;
*s = t;
s++;
temp--;
}
return result;
}
int main()
{
char *str;
str = malloc(50);
printf("Enter a string: ");
scanf("%s", str);
printf("%s\n\n", strrev(str));
return 0;
}
while (*temp)
是什么意思?这合法吗? - Robert Harveystd::reverse
将是唯一正确的答案。 - Jan Hudec