Reverse an array without using iteration

前端 未结 6 1703
走了就别回头了
走了就别回头了 2020-12-07 13:43

A question was asked to me today and I do not believe it is possible, but I could be wrong or am over thinking it. How can you reverse an array without using iteration in C?

6条回答
  •  孤城傲影
    2020-12-07 14:35

    Use recursive function.

    void reverse(int a[],int start,int end)
    {
         int temp;
         temp = a[start];
         a[start] = a[end];
         a[end] = temp;
    
    
        if(start==end ||start==end-1)
           return;
        reverse(a, start+1, end-1);
    }
    

    Just call the above method as reverse(array,0,lengthofarray-1)

提交回复
热议问题