Assignment: Palindrome Checker Using Stack and Queues

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"));

}

}


Last edited on
Hi,

Here are some suggestions which will greatly improve your question+answer experience ;D

1. Code tags. [ code ] ... [ /code ] (No spaces between brackets)
2. Did you have to make Stack/Queue files yourself? If they're known to be good then we can probably ignore that
3. (Most importantly) What is the problem/question you are having in particular?


Edit: is this all supposed to be just C? Can you use C++?
Last edited on
I think you pretty much have it. I guess your custom structures have no way to specify type? Here's what I came up with (changed method signature slightly from char* to const char* for easier string->c string conversion):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int isPalindrome(const char* x)
{
    stack<char> st;
    queue<char> qu;
    
    // Load up structures
    unsigned int pos = 0;
    char c = x[pos];
    while (c != '\0')
    {
        if (isalpha(c))
        {
            c = toupper(c);
            st.push(c);
            qu.push(c);
        }
        c = x[++pos];
    }
    
    // Start popping
    while (!st.empty())
    {
        if (st.top() != qu.front())
            return 0;
        st.pop();
        qu.pop();
    }
    return 1;
}

Edit: cleaned slightly by ridding the first while loop of negation and 'continue'.

Fuller, working, example: https://repl.it/repls/DarkblueCompleteManagement

                    Hello, world! : false
       A dog, a panic in a pagoda : true
   A dog, a plan, a canal, pagoda : true
   Aman, a plan, a canal--Panama! : true
                            civic : true
                 If I had a hi-fi : true
                Do geese see God? : true
               Madam, I’m Adam. : true
      Madam, in Eden, I’m Adam. : true
 Neil, a trap! Sid is part alien! : true
                Never odd or even : true
               No devil lived on. : true
               No lemons, nomelon : true
                          racecar : true
                          RACEcar : true
       Rats live on no evil star. : true
         Red rum, sir, is murder! : true
               Rise to vote, sir. : true
                          rotator : true
                            rotor : true
                 Step on no pets. : true
              Was it a cat I saw? : true
     Was it a car or a cat I saw? : true
           Yawn a more Roman way. : true
Last edited on
Topic archived. No new replies allowed.