This is my code to reverse a string using std::string. But it does not work..
#include <string>
#include <iostream>
using namespace std;
main()
{
string input;
int i, j;
cout << "Enter a string: ";
getline(cin,input);
string output;
for(i = 0, j = input.length() - 1; i < input.length(); i++, j--)
output[i]=input[j];
cout << "Reversed string = " << output;
cin.get();
}
But if we replace string output as char output[100]; it works. So std::string does not allow character assignments?
You have to resize output:
or initially set length:
See also:
std::string