5.11 LAB: Palindrome A palindrome is a word or a phrase that is the same when read both forward and backward. Examples are: "bob," "sees," or "never odd or even" (ignoring spaces). Write a program whose input is a word or phrase, and that outputs whether the input is a palindrome.

Respuesta :

Limosa

Answer:

The following are the program in the C++ Programming Language.

// set the header file

#include <iostream>

#include <string>

//set the namespace

using namespace std;

//declare boolean type function

bool Palindrome(string s)  

{

 //set the for loop  

 for (int i = 0; i < s.length(); ++i)  

 {

   //check that s[i] is not equal to length

   if (s[i] != s[s.length() - i - 1])  

   {  

     //then, return false

     return false;

   }

 }

 return true;

}

//declare the main function

int main()  

{

 //declare string type variable

 string st;

 //get string type input in that variable

 getline(cin, st);

 //check the following function is returns true

 if (Palindrome(st))  

 {

   //then, return the following result with message

   cout << st << " is a palindrome" << endl;

 }  

 //otherwise

 else  

 {

   //return the following result with message

   cout << st << " is not a palindrome" << endl;

 }

 return 0;

}

Output:

bob

bob is a palindrome

Explanation:

The following are the description of the program.

  • Set the required header files and namespace then, declare function 'Palindrome()' and pass the string type argument 's' in its parameter.
  • Then, set the for loop statement that iterates from 0 and end at the length of the string, set the if conditional statement that checks the s[i] is not equal to its length, then it returns false otherwise returns true.
  • Finally, declare the main function in which we set the string data type variable 'st' and get the string data type input from the user in it.
  • Then, set the if conditional statement that checks the following function returns true or false.