Reverse a String

Tags : geeksforgeeks, string, cpp, easy

You are given a string s. You need to reverse the string.

Your Task:

You only need to complete the function reverseWord() that takes s as parameter and returns the reversed string.

Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)

Examples #

Example 1:

Input:
s = Geeks
Output: skeeG

Example 2:

Input:
s = for
Output: rof

Constraints #

Solutions #


class Solution{
  public:
 string reverseWord(string str){
    
  //Your code here
  int n = str.length();
  for(int i = 0; i < n / 2; i++) {
      swap(str[i], str[n - 1 - i]);
  }
  return str;
}
};