Assignment 2 - C++ Palindrome Description The challenge for this second assignme
ID: 3878288 • Letter: A
Question
Assignment 2 - C++ Palindrome Description The challenge for this second assignment is to develop a program, called palindrome.cpp, capable of detecting palindromes. A palindrome is a word, phrase, or sequence that reads the same backward as forward. Your program should take input from stdin and determine whether the characters formulate a palindrome Requirements The following functions should be defined: bool isPalindrome (string text) Example $/palindrome Enter Text: bob The word "bob" is a palindrome. Enter Text: A car, a man,a maraca The phrase "A car, a man, a maraca" is a palindrome. Enter Text: slow The word "slow" is not a palindrome. Enter Text: quit Due: January 29th, 2017 11:59 PMExplanation / Answer
#include <iostream>
#include <conio.h>
#include <string>
#include <cctype>
using namespace std;
bool isPalindrome (string str);
int main()
{
string word;
string str;
cout << "Enter a word to test if it is a palindrome. ";
cin >> str;
for (int i=0; word [i] != ''; i++)
{ word[i] = tolower (word[i]); }
isPalindrome(word);
if (isPalindrome(word) == 1)
{
cout << "The word "<<str<<" is palindrome." << endl;
}
if (isPalindrome(word) == 0)
{
cout << "The word "<<str<<" is not a palindrome." << endl;
}
getch();
return 0;
}
bool isPalindrome(string str)
{
int length = str.length();
for (int i = 0; i < (length / 2); i++)
if (str[i] != str[length - 1 - i])
return false;
return true;
}
Output:
Enter Text: madam
The word madam is a palindrome.
Enter Text: hai
The word hai is not a palindrome.