Cpp - Write program to check whether the word is a palindrome

Requirements

Write a C++ program that reads a word from the keyboard, stores it in a string.

Checks whether the word is a palindrome.

A palindrome reads the same from left to right as from right to left.

The following are examples of palindromes:"OTTO, " "deed, " and "level."

Use the subscript operator [].

Continually read and check words.

Demo

#include <iostream> 
#include <string> 
using namespace std;

int main() {//  w w w  .j  a  v a  2  s .c o m
  string word;                        // Empty string 
  char key = 'y';

  while (key == 'y' || key == 'Y')
  {
    cout << "\n Enter a word: ";
    cin >> word;
    int i = 0, j = word.length() - 1;
    for (; i <= j; ++i, --j)
      if (word[i] != word[j])
        break;

    if (i > j) // All characters equal? 
      cout << "\nThe word " << word << " is a Palindrome !" << endl;
    else
      cout << "\nThe word " << word << " is not a palindrome" << endl;

    cout << "\nRepeat? (y/n) ";
    do
      cin.get(key);
    while (key != 'y' && key != 'Y' && key != 'n' && key != 'N');
    cin.sync();
  }
  return 0;
}

Result

Related Exercise