Answers for "How to reverse a string in c++ using while loop"

C++
1

How to reverse a string in c++ using while loop

#include <iostream>
using namespace std;

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

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

Code answers related to "How to reverse a string in c++ using while loop"

Browse Popular Code Answers by Language