字符串反序问题
#include<stdio.h>
void change(char temp1);
void main()
{
char temp1,temp2;
printf("输入字符串:");
scanf("%c",temp1);
change(temp1);
printf("%c",temp1);
}
void change(char temp1)
{
int i;
char temp2;
while(temp1[i] != '\0')
{
temp2[i] = temp1[i];
i++;
}
for (j = 0; j < i; j++)
temp1[j] = temp2[i - 1 - j];
temp2[j] = temp1[i];
}
错在哪里了呢???
[解决办法]
#include<stdio.h>void change(char *temp1,char *temp2);void main(){ char temp1[20],temp2[20]; //字符串用字符数组来存储 printf("输入字符串:"); scanf("%s",temp1); //输入字符串是%s,系统会自动加'\0' change(temp1,temp2); printf("%s",temp1); //Here %s}void change(char *temp1,char *temp2) { int i=0,j; //i要初始化// char temp2; temp2是局部变量,注意其生存周期 while(temp1[i] != '\0') { temp2[i] = temp1[i]; i++; } for (j = 0; j < i; j++) temp1[j] = temp2[i - 1 - j]; temp2[j] = temp1[i];}