Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Trying to do this in C++ Question 2: A palindrome is a word, which reads the sam

ID: 3590443 • Letter: T

Question

Trying to do this in C++

Question 2: A palindrome is a word, which reads the same backward or forward. For example, noon, civic, radar, level, rotor, kayak, reviver, racecar, redder, madam, and refer are all palindromes a. Implement a function: bool isPalindrome (string str) This function is given a string str containing a word, and returns true if and only if str is a palindrome. b. Write a program that reads a word from the user and announces to the user if it is a palindrome or not. Your program should interact with the user exactly as it shows in the following example: Please enter a word: leve evel is a palindrome

Explanation / Answer

#include <iostream>

#include <string>

using namespace std;

bool isPalindrome(string str){

bool flag = true;

int length = str.length();

  

//this loop matches first and last character, second and second last char and so on,

//if matching fails, it returns false

for(int i=0; i < length; i++){

if(str[i] != str[length-i-1]){

flag = false;

break;

}

}

return flag;

}

int main()

{

string word;

cout << "Please enter a word : " ;

cin >> word;

//calling isPalindrome function

bool test = isPalindrome(word);

if(test){

cout << word << " is a palindrome";

}

else

{

cout << word << " is not a palindrome";

}

return 0;

}

++++++++++++++++++++++++++++++++++++

Sample Output :

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

Hope it helps.

Happy Learning!!!