我试着去让这个code递归,但由于某种原因,它不工作 [英] Im trying to make this code recursive but for some reason it doesnt work
本文介绍了我试着去让这个code递归,但由于某种原因,它不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试让这个code递归但由于某种原因它不工作!任何想法??
im trying to make this code recursive but for some reason it doesnt work ! any ideas ??
void compress_spaces(char *str)
{
char *dst = str;
for (; *str; ++str) {
*dst++ = *str;
if (isspace(*str)) {
do ++str; while (isspace(*str));
--str;
}
}
*dst = 0;
}
的编辑:的
我想这样的:
I tried this:
void text_r(char *str)
{
char *dst = str;
if(*str=='\0')return ;
*dst++ = *str;
if (isspace(*str)) {
do ++str; while (isspace(*str));
--str;
}//Missing brace from orig is this ok?
return text_r(str++);
}
由于某种原因>>什么想法没有工作? :)
didn't work for some reason >> any ideas ? :)
推荐答案
您DST指针是不是在递归调用函数相同的指针,把它作为一个参数来代替。
Your dst pointer is not the same pointer in the recursively called function, pass it as an argument instead.
void text_r(char *dst, char *str) {
if (*str=='\0')
return;
*dst++ = *str;
if (isspace(*str)
while (isspace(*str++));
else
++str;
return text_r(dst, str);
}
为什么你想用递归这样做完全是超越我的方式,这不仅浪费时间和空间。
Why you want to do this with recursion is completely beyond me by the way, it only wastes time and space.
这篇关于我试着去让这个code递归,但由于某种原因,它不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文