Sorry for the noob question, I\'m a newbie programmer and transitioning from C to C++. I could easily write a program to reverse a string in C the same way with minor change
string p; doesn't have enough allocated space for directly accessing by something like p[j]
string p;
p[j]
You can change to initialize p from copying s like below, your code will work.
string s; getline(cin,s); string p(s); // p will be allocated and be the same as s