StackUser
StackUser

Reputation: 1612

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; 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...

Upvotes: 0

Views: 111

Answers (2)

stuck
stuck

Reputation: 1570

Just do not use return. Use stringReverse(&stringa[1]); instead of return stringReverse(&stringa[1]);

Because, the 'return' statement is using for return a value. But your function is void type, that means it returns nothing, it doesn't need.

Upvotes: 1

Matt Lacey
Matt Lacey

Reputation: 8255

return returns a value from the function and performs no further statements in that scope. Have a think about where you want that statement to be...

Not answering in full because this sounds like homework!

Upvotes: 2

Related Questions