Reverse a string using a recursive function

后端 未结 5 1869
悲哀的现实
悲哀的现实 2021-01-22 09:10

I am currently studying C and I can\'t get past this exercise. I must create a recursive function to reverse string1 into string2. Here is my code. I w

5条回答
  •  孤街浪徒
    2021-01-22 09:47

    The reversing starts by copying the n-th character of string1 array into string2. The n-th character happens to be the null terminator. It becomes the first character of your new string, so the string looks empty to all standard C routines, including printf.

    Calling

    reverse(string1,string2,n-1,j);
    

    from the main should fix the problem. The condition in the reverse should be changed from if(n>0) to if(n>=0) as well.

提交回复
热议问题