求助,,,反转字符串,看看那位错了
#include<stdio.h>
char reverse(char str[],int start,int end)
{
char temp;
printf("Original string is:\n%s\n",str);
while(start<end)
{
temp=str[start];
str[start] =str[end];
start++;
str[end]=temp;
end--;
}
printf("Reversed string is:\n%s\n",str);
return 0;
}
main()
{
char str[]={0};
int start,end;
scanf("%s",str);
scanf("%d%d",&start,&end);
reverse(str,start,end);
return 0;
}
[解决办法]
你要实现的功能是反转字符串,那么说我输入一个字符串就行了,你还要别人输入scanf("%d%d",&start,&end);,有点麻烦,
参考:
#include<stdio.h>
void reverse(char orig[])
{
int length = 0;
int i,j;
char temp;
while(orig[length] != '\0')
length ++;
j = length - 1;
for(i = 0; i < length / 2; ++i,--j)
{
temp = orig[i];
orig[i] = orig[j];
orig[j] = temp;
}
}
void main()
{
char orig[20];
printf("Original string is:");
scanf("%s",orig);
reverse(orig);
printf("Reversed string is:%s\n",orig);
}