字符数组的逆序有关问题
字符数组的逆序问题
实现简单将字符数组abcde变成edcba,我自己弄了个C程序在Turbo C for Windows
可以运行,但是结果不对,请问哪里有错呀 请高手指教!
char stringchange(char str[],int n)
{ int i,count,temp;
count=n/2;
for(i=0;i <count;i++)
temp=str[i];
str[i]=str[n-i-2];
str[n-i-2]=temp;
return;
}
#include "stdio.h "
#include "string.h "
main()
{int i;
char a[6]={ 'a ', 'b ', 'c ', 'd ', 'e ', '\0 '};
stringchange(a,6);
for(i=0;i <6;i++)
printf( "%c\n ",a[i]);
}
Answers
#include <stdio.h>
#include <string.h>
char* stringchange(char* str,int n)
{
int i, count, temp;
count = n/2;
for(i=0; i <count; i++)
{
temp = str[i];
str[i] = str[n-1-i];
str[n-1-i] = temp;
}
return str;
}
int main()
{
int i;
char a[6]={ 'a ', 'b ', 'c ', 'd ', 'e ', '\0 '};
stringchange(a, strlen(a));
printf( "%s\n ",a);
return 0;
}