The assignment is to write a program that does the following. You will need to define extra functions to
complete some of these tasks such as the first two bullet items:
Prompts the user to type in a character string
Gets the characters and stores them in a stack data structure.
Makes use of the stack and a queue data structure to check for a palindrome.
Defines a function “isPalindrome” that determines whether the string is a palindrome. The function
“isPalindrome” returns 1 if x is a palindrome, and 0 if x is not a palindrome.
int isPalindrome(char *x)
Displays an appropriate message on the screen that indicates the string and whether the string is a
palindrome or not.
I have come up with the following code, heres what I have some more.
Heres the link to my assignment, where the stack.h and the queue.h files are included.
https://www.dropbox.com/s/mawyte4ivhadipw/CSC260_P5-Palindrone_StacksQueues.pdf?dl=0
//Palindrome.h
#ifndef _PALINDROME
#define _PALINDROME
// method protocol declaration
int isPalindrome(char *isPl);
#endif
//Palindrome.c
#include "Palindrome.h"
#include "stackh"
#include "queue.h"
// method to check the input is palindrome or not
int isPalindrome(char *isPl)
{
// declare the required variables
int lp = 0;
stack palstk;
queue palqu;
// code to check the palindrome
while( isPl[lp] != '\0' )
{
if(isalpha(isPl[lp]))
{
palstk.push(toupper(isPl[lp]));
palqu.push(toupper(isPl[lp]));
}
lp++;
}
while(!palstk.empty())
{
if(palqu.front()==palstk.top())
{
palqu.pop();
palstk.pop();
}else
{
return 0;
}
}
return 1;
}
//main.c
#include "Palindrome.h"
#include <stdio.h>
// main method to invoke the functions to test palindrome
int main()
{
//Character array with a set of strings
char *palcheck[] = {
"Hello world", "A dog, a panic in a pagoda",
"A dog, a plan, a canal, pagoda",
"A man, a plan, a canal?? Panama!",
"civic",
"No lemons, no melon",
"racecar",
"RACEcar",
"Rats live on no evil star.",
"Red rum, sir, is murder!",
"Rise to vote, sir.",
"rotator",
};
// loop code to check each string is palindrome or not
for(int lp=0;lp<12;lp++)
{
printf("%s:",palcheck[lp]);
printf("%s\n",isPalindrome(palcheck[lp])?printf("yes")
: printf("no"));
}
}