My implementation is correct but it is not getting me the answer

@akash.91.yadav It is not correct, check for a simple string = “aa”.
Check for some string by drying running it, may be you have not implemented it the way you thought.

But sir if do some changes as i have done it shows the correct answer but is not in the main string https://ide.codingblocks.com/s/233020

@akash.91.yadav You dont need to do the null termination, because c++ stl strings are not terminated by using null character. Null termination is only for character arrays.
Also pass string by refernce if you want to have to print it in Main function.
I have done the these changes to explain better, you can have a look at code if you need https://ide.codingblocks.com/s/233152

Also please mark this doubt as resolved if i am able to solve your doubt.