You pass strings by value, which means that only a local copy of the string will be undone in the reverse function. You must pass them by reference.
Also, do not modify direct memory directly. Use operator[] as follows:
for (size_t beg = 0, size_t end = str.size() - 1; beg < end; ++beg, --end) str[beg] = str[end];
So, all together:
void reverse(string& str); // prototype .... void reverse(string& str) { // note the & which is pass by reference int length = str.size(); for (size_t beg = 0, size_t end = str.size() - 1; beg < end; ++beg, --end) str[beg] = str[end]; }
And as Mooing Duck pointed out, the place where you are likely to crash is looking for a pointer that has garbage value here:
char * temp; *temp = ...
You are trying to assign some random memory to a value that is likely to rob you.
source share