Create three functions to determine if a string is a palindrome, which is a stri
ID: 3828474 • Letter: C
Question
Create three functions to determine if a string is a palindrome, which is a string that reads the same forward and backward (assume case matters). create a string object, ask the user for input to the string via keyboard input, call the three functions below, and display the results to the screen. pass the string as a const value parameter. Write a loop using [J (or .at()) and the length() method to determine if the string is a palindrome. Return true if the string is a palindrome, false otherwise. pass the string as a const value parameter. Use recursion to determine if the string is a palindrome. You may pass additional parameters as needed. Return true if the string is a palindrome, false otherwise. pass the string as a const value parameter. Loop through the string and push each character to a stack and to a queue (use the and libraries). Use the stack and queue to determine if the string is a palindrome -if it's not apparent how to do this, try drawing the stack and queue. Return true if the string is a palindrome, false otherwise. For extra practice: use iterators for Function 1 - there are various ways to do this, one of which provides a "shortcut" to solve the problem. On Canvas, turn in your source file with all three functions implemented and tested. If working in pairs, please note whom you worked with; you should each turn in a copy of your shared solution. These will not be as closely graded as full labs. For full credit, you should have at least two working functions.Explanation / Answer
PROGRAM CODE:
#include <iostream>
#include <stack>
#include <queue>
#include <string.h>
using namespace std;
bool iterPal(const string word)
{
for(int i=0, j=word.length()-1; i<word.length() && j>=0; i++ )
{
if(tolower(word[i]) != tolower(word[j]))
{
return false;
}
if(i==j)
break;
j--;
}
return true;
}
bool recurPal(const string word, int i, int j)
{
if(i==j)
{
if(tolower(word[i]) == tolower(word[j]))
return true;
else return false;
}
if(tolower(word[i]) == tolower(word[j]))
return recurPal( word, ++i, --j);
else return false;
}
bool stackQueuePal(const string word)
{
stack<char> stack;
queue<char> queue;
for(int i=0; i<word.length(); i++)
{
stack.push(word[i]);
queue.push(word[i]);
}
for(int i=0; i<queue.size(); i++)
{
if(tolower(queue.front()) != tolower(stack.top()))
{
return false;
}
else
{
queue.pop();
stack.pop();
}
}
return true;
}
int main() {
string palWord = "Madam";
string notPalWord = "Hello";
if(iterPal(palWord))
cout<<palWord<<" is a palindrome."<<endl;
else cout<<palWord<<" is not a palindrome."<<endl;
if(iterPal(notPalWord))
cout<<notPalWord<<" is a palindrome."<<endl;
else cout<<notPalWord<<" is not a palindrome."<<endl;
if(recurPal(palWord, 0, 4))
cout<<palWord<<" is a palindrome."<<endl;
else cout<<palWord<<" is not a palindrome."<<endl;
if(recurPal(notPalWord, 0, 4))
cout<<notPalWord<<" is a palindrome."<<endl;
else cout<<notPalWord<<" is not a palindrome."<<endl;
if(stackQueuePal(palWord))
cout<<palWord<<" is a palindrome."<<endl;
else cout<<palWord<<" is not a palindrome."<<endl;
if(stackQueuePal(notPalWord))
cout<<notPalWord<<" is a palindrome."<<endl;
else cout<<notPalWord<<" is not a palindrome."<<endl;
return 0;
}
OUTPUT:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.