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 changes but writing this in C++, why does this not print anything:
#include <iostream> #include <string> using namespace std; int main(){ string s,p; getline(cin,s); int j=0,i = 0; while(s[i]!='\0'){ i++; } i--; while(i!=-1){ p[j] = s[i]; j++; i--; } cout << p << endl; return 0; } if i replace the p with say p[2], it correctly prints out the reverse 3rd character of the original string, but i cant find a way to print the whole string.
std::stringis not NUL terminated, either. Use the Standard Library functions for things likesize.p = s;string, you can do:s.size()