By using C A word is a palindrome if it has the property of

By using C++

A word is a palindrome if it has the property of reading the same forward as backward. For example, “madam” is a palindrome since it can be read the same left-to-right of right-to-left.

Write a bool method that determine whether or not a given word bbWord (a String) is a palindrome. The name of this method is isPalindrome.

Example:

       If the method was called by the following line of C++ :

               Bool isPal = isPalindrome (“madam”) ;

         Then the variable isPal would contain the value :

                     true

Hint : bbWord can be a String of any length, so you must use a loop.

Your code could compare the first letter to the last letter, then compare the second letter to the second letter to the second-last letter, etc.

Solution

Please find the required program along with its output. Please see the comments against each line to understand the step.


#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

bool isPalindrome(string bbWord){ //function to check for the given string is palindrom or not
const char * str = bbWord.c_str(); //convert the given string to char * constant, inorder to use in strlen
bool flag = true;
int l = strlen(str); //get length of string
for(int i=0; i < l; i++){ //iterate through all element in the string
if(bbWord[i] != bbWord[l-i-1]){ //if the i\'th element is not equals with its counterpart from other end, then make flag false and exit the loop
flag = false;
break;
   }
   }
   return flag; //return the flag
}

int main()
{
bool isPal = isPalindrome (\"madam\") ; //call to check palindrom
  
if(isPal) //if the return value is true, then the given string is palindrome else not
cout << \"true\" << endl;
else
cout << \"false\" << endl;

}

------------------------------------------------------

OUTPUT:

true

By using C++ A word is a palindrome if it has the property of reading the same forward as backward. For example, “madam” is a palindrome since it can be read th
By using C++ A word is a palindrome if it has the property of reading the same forward as backward. For example, “madam” is a palindrome since it can be read th

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site