How to reverse a string in c++ using reverse function
#include <iostream>
//The library below must be included for the reverse function to work
#include<bits/stdc++.h>
using namespace std;
int main() {
string greeting = "Hello";
//Note that it takes the iterators to the start and end of the string as arguments
reverse(greeting.begin(),greeting.end());
cout<<greeting<<endl;
}
// C++ program to illustrate the
// reversing of a string using
// reverse() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str = "geeksforgeeks";
// Reverse str[begin..end]
reverse(str.begin(), str.end());
cout << str;
return 0;
}
class Solution {
public:
void reverseString(vector<char>& s) {
int i=0;
int j=s.size()-1;
while(i<j)
{
swap(s[i],s[j]);
i++;
j--;
}
}
};
//Runtime: 20 ms, faster than 76.31% of C++ online submissions for Reverse String.
//Memory Usage: 23.3 MB, less than 38.31% of C++ online submissions for Reverse String.