Your string operations are very confusing and should be slow. You better use the
std::string class with its nice functions like
for input.
If something in your code is unclear you should set a break point and use the debugger.
tips:
a) use identions for better readability
b) you better write some comments in your code to make it better understandable.
c) overwriting input data is not a good idea, generate a output string.