简体   繁体   English

c:使用递归反转字符串?

[英]c: invert string using recursion?

What is wrong in this function? 此功能有什么问题?

   void stringReverse (char stringa[])
    {
        if (stringa[0]!='\0')
        {
            return stringReverse(&stringa[1]);
            printf("%c", stringa[0]);
        }
    }

I have to write a function that invert a string (ex: "Hello" in "olleH") using recursion; 我必须编写一个使用递归来反转字符串的函数(例如:“ olleH”中的“ Hello”); the function has to receive a string (nothing else) and to print the character in the inverse order... i don't understand why what i write didn't print anything... 该函数必须接收一个字符串(没有其他内容)并以相反的顺序打印字符...我不明白为什么我写的内容没有打印任何内容...

return returns a value from the function and performs no further statements in that scope. return从函数返回一个值,并且在该范围内不执行其他任何语句。 Have a think about where you want that statement to be... 考虑一下您希望该语句在哪里...

Not answering in full because this sounds like homework! 没有完全回答,因为这听起来像是作业!

Just do not use return. 只是不要使用return。 Use stringReverse(&stringa[1]); 使用stringReverse(&stringa[1]); instead of return stringReverse(&stringa[1]); 而不是return stringReverse(&stringa[1]);

Because, the 'return' statement is using for return a value. 因为,“ return”语句用于返回值。 But your function is void type, that means it returns nothing, it doesn't need. 但是您的函数是void类型,这意味着它什么也不返回,不需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM