#include<stdio.h>
#include<stdlib.h>
int main()
{
char s[200],*p,t,*q;
p=(char *)malloc(200);
printf("请输入一个字符串:");
scanf("%s",s);
p=s,q=s;
while(*q!='\0') q++;
for(;p<=q;p++,q--)
{
t=*p;*p=*q;*q=t;
}
printf("该字符串逆反后为%s! \n",p);
free((void *)p);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int main()
{
char s[200],*p,t,*q;
printf("请输入一个字符串:");
scanf("%s",s);
p=s,q=s;
while(*q!='\0')
q++;
q--;
for(;p<=q;p++,q--)
{
t=*p;*p=*q;*q=t;
}
printf("该字符串逆反后为%s!\n",s);
return 0;
}
while(*q!='\0') q++;这里q已经不是你输入的字符串内容了,而是字符的结束符了,在for循环逆序前,q需要先回退一下;
在while(*q!='\0') q++这一句下面加一句,q--;
while(*q!='\0') q++;
q--; //这里回退一下,才是你输入的字符的最后一个字符
//其余的原来代码
另外,你的p不需要在申请空间,p指向s了,p=(char *)malloc(200);这一句删掉就可以啦