palindrome check programe using stack

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xiaolim
    New Member
    • Jul 2008
    • 18

    palindrome check programe using stack

    hi, sorry to disturb again, currently i have an assignment on stacks and as show in the the title, i need to use stacks to determine palindrome.

    i've done part of my code, here it is:
    Code:
    // Write your name, student number, part-time/full time, degree here
    #include <string>
    #include <iostream>
    #define STACKSIZE 80
    #define TRUE 1
    #define FALSE 0
    using namespace std;
    
    class Stack 
    {
       public:
              void StackInit();
              int IsStackEmpty();
              int IsStackFull();
              int StackPush(char c);
              char StackPop();
              char ViewStackTop();
       private:
              char myStack[80];
              int top;
    };
    
    string pstring;
    
    int main()
    {
        // write your code here
        cout << "This Is A Palindrome Check Programe" << endl;
        cout << "Please Enter Anything For Palindrome Check" << endl;
        
        cin >> pstring;
        
    
        
        
        system("pause");
        return 0;
    }
    
    void Stack::StackInit()
    {
      top = -1;
    }
    
    int Stack::IsStackEmpty()
    {
      if (top == -1)
        return TRUE;
      return FALSE;
    }
    
    int Stack::IsStackFull()
    {
      if (top == (STACKSIZE - 1))
        return TRUE;
      return FALSE;
    }
    
    int Stack::StackPush(char c)
    {
      if (top == (STACKSIZE - 1))
        return FALSE;
      myStack[++top] = c;
      return TRUE;
    }
    
    char Stack::StackPop()
    {
      if (top == -1)
        return '\0';
      return myStack[top--];
    }
    
    char Stack::ViewStackTop()
    {
      if (top == -1)
        return '\0';
      return myStack[top];
    }
    alright, i am stuck again, i need to ignore space, punct and digits, i know using isdigit / ispunct / isspace. however i ahh.. duno where to put them. hmmm, simple guide will do. thanks, meanwhile, i will be doing search on google as well. please help. thanks
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    It looks like you need an algorithm. All I see is some stack code, which is not complete.

    Put a loop in main(). Create a stack and then parse the string from left to right pushing each letter or number onto the stack (use isalnum to determine a letter or a number).

    Then create a second stack and oparse the string from right to left pushing each letter or number onto the stack.

    Finally, in a third loop pop each stack and compare letters. If not equal, you do not have a palindrome. If equal, pop them both again. If the letters remain equal when the stacks are empty, you have a palindrome.

    Note that the loops could be replaced by recursive calls. But since this might produce a lot of nested calls, loops are more efficient.

    Then post agian.

    Comment

    • xiaolim
      New Member
      • Jul 2008
      • 18

      #3
      Code:
      #include <ctype.h>
      #include <stdio.h>
      #include <string.h>
      #include <iostream>
      #define STACKSIZE 80
      #define TRUE 1
      #define FALSE 0
      using namespace std;
      
      class Stack 
      {
         public:
                void StackInit();
                int IsStackEmpty();
                int IsStackFull();
                int StackPush(char c);
                char StackPop();
                char ViewStackTop();
         private:
                char myStack[80];
                int top;
      };
      
        
      char input[100], output[100];
      Stack s;
      
      int main()
      {
          char c;
          s.StackInit();
          // write your code here
          cout << "This Is A Palindrome Check Programe" << endl;
          cout << "Please Enter Anything For Palindrome Check" << endl;
          fflush(stdin);
          cin.getline(input, 100);
          
          for (int i=0;i<strlen(input);i++)
          
      
          //check input is upper and convert to lower
          while (input[i])
          {
          c=input[i];
          putchar (tolower(c));
          i++;
          }
          
          //check input have punct or space
          if (isspace(input[i]))
          
          
          
          //pass converted string to a temp
          c = input[i];
      
          //check input is only alpha and push to stack
          if(isalpha(input[i]))   
          s.StackPush(input[i]);
          i++;
      
          
          //compare 1st 2 char with last 2 char
          if strcmp( ?????? )
          
          //output
          cout << "The String " << c << "Is A Palindrome." << endl;
          
          else
      
          cout << "The String " << c << "Is Not A Palindrome." << endl;    
          
          
          
          system("pause");
          return 0;
      }
      
      void Stack::StackInit()
      {
        top = -1;
      }
      
      int Stack::IsStackEmpty()
      {
        if (top == -1)
          return TRUE;
        return FALSE;
      }
      
      int Stack::IsStackFull()
      {
        if (top == (STACKSIZE - 1))
          return TRUE;
        return FALSE;
      }
      
      int Stack::StackPush(char c)
      {
        if (top == (STACKSIZE - 1))
          return FALSE;
        myStack[++top] = c;
        return TRUE;
      }
      
      char Stack::StackPop()
      {
        if (top == -1)
          return '\0';
        return myStack[top--];
      }
      
      char Stack::ViewStackTop()
      {
        if (top == -1)
          return '\0';
        return myStack[top];
      }
      alright, i tried, i briefly list out what i should do.

      1. StackInit.
      2. promt user to enter anything.
      3. check if is uppercase, if yes, convert to lowercase then move to next char
      4. ignore all space and punct.
      5. pass the converted string to another temp var.
      6. check if is only alpha, if yes, push char in stack. if not, ignore go to next char
      7. pop the last 2 char.
      8. compare 1st 2 char with last 2 char, if is similar, then output result, if not, display msg.

      so, quite alot of problem, maybe someone can help me check out n tell me where i go wrong? i keep can't compile on the "tolower" part. thanks alot

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        OK, I see where you are trying to build a stack of the letters as you go from left to right.

        Where is the stack for the letters when you go from right to left? I see only one stack.

        When you get to strcmp(????) both stacks need to be complete. You then pop from eaxch stack and if the two stacks are identical, you have a palindrome.

        Comment

        • xiaolim
          New Member
          • Jul 2008
          • 18

          #5
          Code:
          int main()
          {
              char input[100], output[100];
              Stack s1;
              Stack s2;
              Stack s3;
              s1.StackInit();
              
              // write your code here
              cout << "This Is A Palindrome Check Programe" << endl;
              cout << "Please Enter Anything For Palindrome Check" << endl;
              fflush(stdin);
              cin.getline(input, 100);
              
          
              for (int i=0;i<strlen(input);i++)
              
              {
               if (isupper(input[i]))
               input[i]=tolower(input[i]);
          
               if (isspace(input[i]))
               i++;
          
               if (ispunct(input[i]))
               i++;
          
               if (isalpha(input[i]))
               s1.StackPush(input[i]) & s2.StackPush(input[i]);
               
               }
               
               
               
                cout << "Reverse String: ";
                while (!s1.IsStackEmpty())
                cout << s1.StackPop();
                cout << "" << endl;
              
              
              
              system("pause");
              return 0;
          }
          alright, i' ve made some changes in my code. As shown in the code, i declared 3 stack, then i push input to stack1 & stack2 and get them back in reverse order. so now i want to push the reversed string to stack3 and then do a compare if they matches or not. so how do i push them back?

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            I don't think you've got the idea yet.

            Take the palimdrome: Madam, I'm Adam.

            First, convert to lower case: madam i'm adam
            Second: Replace all punctuation with a space: madam i m adam
            Third: Create two stacks.
            Fourth: Stack one push from the left to right
            m
            a
            d
            a
            m
            i
            m
            a
            d
            a
            m

            Fifth:Stack two push from the right to left:
            m
            a
            d
            a
            m
            i
            m
            a
            d
            a
            m

            Now can you see the two stacks are the same? You test this by popping each stack and comparing letters. If the letters match, pop again and repeat. When the stacks are empty and the letters always matchedm, then you have a palinrome.

            There's no third stack.

            Comment

            • hassanqbl67
              New Member
              • Apr 2010
              • 1

              #7
              This code is very easy and has an easy logic. It contains no functions...... ........

              #include<iostre am>
              using namespace std;
              int main()
              {
              int first;
              int last;
              first=0;
              char arr[100];
              cout<<"\t\t**** ******Palindrom e**********"<<e ndl<<endl<<endl ;
              cout<<"Enter a word to check wether it is a Palindrome: "<<endl<<en dl;
              cin>>arr;
              last=strlen(arr )-1;
              if(arr[first]==arr[last])
              {

              cout<<"It is a Palindrome"<<en dl;
              while(first<100 )
              {
              first ++;
              last --;
              }
              }
              else
              {
              cout<<"It is NOT a Palindrome"<<en dl;
              }
              system("PAUSE") ;
              return 0;
              }

              Comment

              • newb16
                Contributor
                • Jul 2008
                • 687

                #8
                Dug up thread is dug up.

                Comment

                Working...