Back to track
#8 Easy Foundations

String Reverse

Open the editor

Problem

Reverse a string in-place.

Constraints

1 <= |s| <= 200000

Hints

  1. 1

    Two pointers

  2. 2

    Swap chars

Worked solution

Reference solution

One correct implementation. Alternative approaches that satisfy the constraints are equally valid — the reviewer judges behaviour, not style.

#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  string s; cin >> s;
  int l = 0, r = (int)s.size() - 1;
  while (l < r) swap(s[l++], s[r--]);
  cout << s << '\n';
  return 0;
}

© 2026 ChitChat