Terrible Chandu

Chandu is a bad student. Once his teacher asked him to print the reverse of a given string. He took three hours to solve it. The teacher got agitated at Chandu and asked you the same question.

Example:

Input:  s = "ab"
Output: ba

Approach

C++

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

string reverseString(string s)
{
    for (int i = 0i < s.size() / 2i++)
    {
        swap(s[i]s[s.size() - 1 - i]);
    }
    return s;
}
int main()
{

    string s = "ab";

    cout << reverseString(s<< "\n";

    return 0;
}


No comments:

Post a Comment