这是一个不使用strlen()
和strrev()
函数的字符串反转程序。请帮我看看这个代码:length
函数显示11
,但reverse
函数不起作用。为什么这个字符串反向程序不工作?
#include <stdio.h>
#include <conio.h>
int length(char*);
char *reverse(char*);
main() {
printf("%d", length("himanshupal"));
printf("%s", reverse("himanshupal"));
getch();
}
/* used to calculate the lenghth*/
int length(char *p) {
int l;
for (l = 0; *(p+l) != '\0'; l++);
return (l);
}
char *reverse(char *p) { // function for reversing the string
// l used for length
int i, l;
char t;
for (l = 0; *(p+l) != '\0'; l++);
for (i = 0; i < l/2; i++) {
t = *(p+i);
*(p+i) = *(p+l-1-i);
*(p+l-1-i) = t;
}
return (p);
}
功能长度当你说该程序不工作,你这是什么意思呢?你有构建错误吗?然后发布你得到的确切的错误。你有没有崩溃?然后在调试器中运行以找到它发生的位置。你有意想不到的结果吗?然后请告诉我们实际和预期的产出。另请[请阅读如何提出好问题](http://stackoverflow.com/help/how-to-ask)。 –
字符串文字的类型是'const char *'。无论如何,你的程序编译是一个悲剧,每个C程序员都必须学习艰难的道路。 –
我用length.exe保存我的程序。运行后长度函数显示为11,但比绿线运行窗口弹出后显示并且显示length.exe运行不正确 –