【发布时间】:2020-04-07 05:01:08
【问题描述】:
#include<stdio.h>
#include<conio.h> //**Just To Add getch() function**
int length(char *p){
int i; //**I know That these variable are not the same as they are in other function**
for(i=0;*(p+i)!='\0';i++);
return i;
}
void strrev(char *p){
int i,len;
len=length(p);
char cpy[len]; //**Already Tried to change it to some fixed value**
for(i=0;i<len;i++){
cpy[i]=*(p+len-i);
}
for(i=0;i<len;i++){
*(p+i)=cpy[i];
}
}
int main(){
char str[20]="computer";
strrev(str);
printf("%s",str);
getch(); //**to Stop The Screen**
return 0;
}
我尝试将数组大小更改为固定值,我也尝试更改变量,但我的语法没有错误。
【问题讨论】:
-
只需迭代到
len >> 1。试试这个。 -
如果你迭代到字符串的长度,你把它倒过来再倒回去。你只需要走一半。