I have a function that takes a string,and reverses it, using front and rear pointers. Now this is the program written below. what I do not understand is the while loop. When it assigns *front = *rear , and *rear = temp //which is *front.
how do we still increment and decrement front and rear respectively. But we switched them didn't we ? isn't front in the rear and rear in the front now ? or is it because it's pointers ? can someone explain this please to me ?
Program:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void reverse(char* s);
int main()
{
char aString[] = "babylonia";
cout << " please enter the string " << endl;
reverse(aString);
cout << aString << endl;
}
void reverse(char* s)
{
char *front, *rear, temp;
front = s;
rear = s+strlen(s) - 1;
while( front < rear)
{
temp = *front;
*front = *rear;
*rear = temp;
front++;
rear--;
}
}