You are here

C++ program to reverse a string

C++ program to reverse a string that a user will input, for example, if the user enters "hello," then "olleh" will be printed as output.

C++ program

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
  char a[100], b[100];
  int i, j, l;

  cout << "Enter a string to reverse\n";
  cin.getline(a,100); // cin >> a; can be used for a string without a space

  l = strlen(a);

  for (i = l-1, j = 0; i >= 0; i--, j++)
    b[j] = a[i];

  b[j] = '\0';

  cout << "Reverse of the string: " << b << "\n";

  return 0;
}