首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

字符串反序有关问题

2012-04-10 
字符串反序问题#includestdio.hvoid change(char temp1)void main(){char temp1,temp2printf(输入字

字符串反序问题
#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];
}
错在哪里了呢???

[解决办法]

C/C++ code
#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];} 

热点排行