C++ bootcamp · Lab 14

Reverse a word

easyStrings10 minLesson: Strings

Read the question, write C++ on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read one word and print it reversed.

std::reverse edits the string in place — no second string needed.

Input. One line: a word.

Output. The word, reversed.

Examples

Example 1
Input
code
Output
edoc
Example 2 — Every character is reversed, symbols included.
Input
C++
Output
++C
Hint
  1. #include <algorithm> for std::reverse.
  2. std::reverse(word.begin(), word.end());
Show correct code

Peek only after you have tried. You can still Check your own version.

#include <iostream>
#include <string>
#include <algorithm>

int main() {
  std::string word;
  std::cin >> word;
  std::reverse(word.begin(), word.end());
  std::cout << word << "\n";
  return 0;
}
main.cppC++17 · g++ · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.