求助,递归实现字符串逆序存储
解决时间 2021-03-03 21:34
- 提问者网友:wodetian
- 2021-03-02 23:39
用递归实现字符串的逆序存储,不另开辟存储空间~·为什么结果不对那?还有最后释放内存,要在里用free函数啊?谢谢啦~·#include #include #include #define INITSTRLEN 100 typedef struct _string{ char *ch; int length; int strsize;}string; void initstring(string *s){ s->ch = (char *)malloc(INITSTRLEN * sizeof(char)); s->length = 0; s->strsize = INITSTRLEN; } char turn(string *s){ char temp; static int i = 0; if (i <=s->length / 2) { temp = s->ch[i]; s->ch[i] = s->ch[s->length-1-i]; s->ch[s->length-i-1] = temp; i++; turn(s); }
} void list(string *s){
printf("%s", s->ch); printf("\n");} int main(){ string s; initstring(&s); printf("please input the string s:\n"); scanf("%s", s.ch); turn(&s); list(&s); getch();}
最佳答案
- 五星知识达人网友:患得患失的劫
- 2021-03-03 00:46
答案如下,vc6.0运行通过:#include #include
#include
#include
#define INITSTRLEN 100
typedef struct _string{char *ch;int length;int strsize;}string;
void initstring(string *s){s->ch = (char *)malloc(INITSTRLEN * sizeof(char)); s->length = 0; s->strsize = INITSTRLEN; }
void turn(string *s){char temp;static int i = 0;if (i <=s->length / 2){temp = s->ch[i];s->ch[i] = s->ch[s->length-1-i];s->ch[s->length-i-1] = temp;i++;turn(s);}
}
void list(string *s){
printf("%s", s->ch);printf("\n");}
int main(){string s;initstring(&s);printf("please input the string s:\n");scanf("%s", s.ch);
s.length=strlen(s.ch);turn(&s);list(&s);
free(s.ch);
getch();
return 0;}
我要举报
大家都在看
推荐资讯