关于C语言中利用循环使字符倒序的问题

来源:百度知道 编辑:UC知道 时间:2024/05/31 11:27:32
主函数是
main()
{char *p;
gets(p);
exchange(p);
}
exchange(char *p);
利用一个for循环使a b c d e f g七个字母倒序输出
请高手帮帮忙 谢谢

#include <stdio.h>
#include <string.h>
exchange(char *p)
{
int i,n=strlen(p);
char t;
for (i=0;i<n/2;i++)
{
t=p[i];
p[i]=p[n-i-1];
p[n-i-1]=t;
}
}
main()
{char p[i];
gets(p);
exchange(p);
puts(p);
}

albertyn 的程序明显是错的,且错的很离谱

这样写:
#include <stdio.h>
#include <string.h>
exchange(char *p)
{
int i,len=strlen(p);
char t;
for (i=0;i<len/2;i++){
t=p[i];
p[i]=p[len-i-1];
p[len-i-1]=t;
}
}
main()
{char p[200];
gets(p);
exchange(p);
puts(p);
}


char i,x,y;
for (i=0;i<strlen(*P);i++)
{
strcpy(*p[i],x);
strcpy(*p[strlen(*p)-i],y);
strcpy(x,*p[strlen(*p)-i]);
strcpy(y,*p[i]);
}

如果要是可以不改变数组的话,可以这样写,也许会更简单;
#include <stdio.h>
#include <string.h>
#include&l