write a C++ program for the following A palindrome is any word or phrase which r
ID: 3817136 • Letter: W
Question
write a C++ program for the following A palindrome is any word or phrase which reads the same backwards as it does forward. For instance the word Bob is a palindrome. Napoleon is also attributed as saying the famous palindrome Able was I ere I saw Elba. As a counter example the phrase Computer Science is fun is NOT a palindrome. For this exercise you will implement a program which asks the user for a string. The program will then output whether or not the phrase is a palindrome. Hint: use an array of characters to determine if the string is a palindrome. Sample output: Enter a phrase: Rise to vote sir “Rise to vote sir” IS A PALINDROME
Explanation / Answer
Hi, I am trying to provide a compact solution for the above query.
Here the program will execute the exact output which comprises with your requirement.
HAPPY LEARNING :-)
.............................................................
#include <iostream>
#include <string>
using namespace std;
int main()
{
//Variables and Arrays
char Phrase[80];
char Reverse[80];
char* Palindrome = Reverse;
int i, j, test = 1;
cout << "Please enter a sentence to be reversed: ";
cin >> Phrase;
cin.getline(Phrase, 80);
int length = strlen(Phrase);
for(i = 0; i < (length/2); i++) // do a loop from 0 to half the length of the string
{
if(test == 1) // test for palindrome
{
if(Phrase[i] != Phrase[length-i-1]) // check if the characters match
{
test = 0; // if they don't set the indicator to false
}
}
else
{
break; // if it is not a palindrome, exit the for loop
}
}
if(test == 1) //test to print out the phrase if it's a palindrome
{
cout << "Phrase/Word is a Palindrome." << endl;
for(j = strlen(Phrase) - 1; j >= 0; Palindrome++, j--)
{
*Palindrome = Phrase[j];
cout << "The reverse is: " << Reverse << endl << endl;
}
}
else
{
cout << "Phrase/Word is not a Palindrome." << endl;
}
system("Pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.