0

I need my code to be able to store the word in the string s for it to be reversed, but there's something wrong. Please help?

#include <iostream> #include <string> using namespace std; int main() { string s; int i = 0; int j = s.size() - 1; char temp; cout << "Enter a word." << endl; cin >> s ; while (j>i) {; temp = s[j]; s[j] = s[i]; s[i] = temp; ++i; --j; } cout << s; }

4th Jun 2020, 7:22 PM
Eileen Nerima
Eileen Nerima - avatar
3 Antworten
+ 5
What would you do if someone asked you for the length of a string before you knew the actual string? Pretty much what is happening here.
4th Jun 2020, 8:11 PM
Dennis
Dennis - avatar
+ 1
std::reverse(s) might help as well
5th Jun 2020, 1:28 PM
Orville Vroemen
Orville Vroemen - avatar
0
Saw the problem and solved it!
7th Jun 2020, 6:15 AM
Eileen Nerima
Eileen Nerima - avatar