Answers for "reverse c++ string"

C++
0

How to reverse a string in c++ with for loop

#include <iostream>
using namespace std;

int main() {
 
  string greeting = "Hello";
  int len = greeting.length();
  int n=len-1;
  for(int i=0;i<(len/2);i++){
    //Using the swap method to switch values at each index
    swap(greeting[i],greeting[n]);
    n = n-1;

  }
  cout<<greeting<<endl;
}
Posted by: Guest on August-20-2021
-1

reverse string c++

#include <iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string str;
  getline(cin,str);
  reverse(str.begin(),str.end());
  cout<<str;
}
Posted by: Guest on July-06-2021

Browse Popular Code Answers by Language