This is a program to reverse the string in place. I have used pass by pointer. Interestingly I have changed the pointer in reverse function which is notable by seeing its output but the pointer which was passed in main function still points to the start of string. This is quite perplexing as I cannot figure out the cause for the same. Any help is appreciated.
#include <iostream>
#include <sstream>
using namespace std;
void reverse(char* str)
{
cout<<str<<endl;
char*end=str;
if(str==0)
{
cout<<"null string detected"<<endl;
return;
}
int length;
while(*end)
{end++;}
end--;
char temp;
// char* begin=str;
while(str<end)
{
temp=*str;
*str++=*end;
*end--=temp;
}
cout<<str<<endl;
}
int main()
{
try
{
char* str=new char[100]; //char* str=new char[100000000000000000];
cin>>str;
// str=NULL;
reverse(str);
cout<<str<<endl;
delete[] str;
}
catch(exception& e)
{
cout << "Standard exception: "<<e.what()<<endl;
}
}