164k views
2 votes
Write a function called reverse() with this prototype:

void reverse(char dest[], char source[], int size);

Your function should copy all the elementsin the array source into the array dest, except in reverse order. The number of elements in the source array is give in the parameter size. Use a for loop to do the copying. Assume that dest is large enough to hold all of the elements. Please be thorough with commenting.

User Deeenes
by
6.0k points

1 Answer

5 votes

Answer:

void reverse(char dest[], char source[], int size)

{

for(int i=0;i<size;i++)//using for loop.

{

dest[i]=source[i];//assigning each element of source array to dest array.

}

int s=0,e=size-1;//initializing two elements s with 0 and e with size -1.

while(s<e){

char t=des[s]; //SWAPPING

dest[s]=dest[e]; //SWAPPING

dest[e]=t; //SWAPPING

s++;

e--;

}

}

Step-by-step explanation:

I have used while loop to reverse the array.I have initialize two integer variables s and e with 0 and size-1.Looping until s becomes greater than e.

It will work as follows:

first s=0 and e=0.

dest[0] will be swapped with dest[size-1]

then s=1 and e=size-2.

then des[1] will be swapped with dest[size-2]

and it will keep on going till s is less than e.

User DanMunoz
by
6.3k points