how to reverse a string in c 2b 2b with for loop

Solutions on MaxInterview for how to reverse a string in c 2b 2b with for loop by the best coders in the world

showing results for - "how to reverse a string in c 2b 2b with for loop"
Adriana
22 Nov 2019
1#include <iostream>
2using namespace std;
3
4int main() {
5 
6  string greeting = "Hello";
7  int len = greeting.length();
8  int n=len-1;
9  for(int i=0;i<(len/2);i++){
10    //Using the swap method to switch values at each index
11    swap(greeting[i],greeting[n]);
12    n = n-1;
13
14  }
15  cout<<greeting<<endl;
16}