0

my code should check if the Entered string is a palindrome or not

    // stack.h
    typedef char comp;
    struct nodetype;
    typedef nodetype* nodeptr;

    class stack{
    public:

        stack();
        bool isfull()const;
        bool isempty()const;
        void push(comp elm);
        void pop(comp& elm);
        ~stack();

    private:
    nodeptr top;
    };

    /********************************************

    stack.cpp

    */

    #include<iostream>
    #include"stack.h"
    #include<cstddef>
    #include<cstdlib>
    #include<cstring>


    using namespace std;

    struct nodetype
    {
        comp data;
        nodeptr link;
    };

    stack::stack()
    {
    top=NULL;
    }
    bool stack::isempty()const
    {
    return top==NULL;
    }

    bool stack::isfull()const
    {
    nodeptr temp=new nodetype;
    return temp==NULL;
    }

    void stack::push(comp elm)
    {
    if(isfull())
        cout<<"No space ...!"<<endl;
    else
    {
    nodeptr newptr=new nodetype;
    newptr->data=elm;
    newptr->link=top;
    top=newptr;
    }

    }

    void stack::pop(comp& elm)
    {
    if(isempty())
    cout<<"NO DATA ...!"<<endl;
    else
    {
        nodeptr del=top;
        elm=top->data;
        top=top->link;
        delete del;

    }


    }

    stack::~stack()
    {
        nodeptr del;
    while(top!=NULL)
    {
    del=top;
    top=top->link;
    delete del;

    }

    }

    /**********************************

    client file

    */

    #include<iostream>
    #include"stack.h"
    #include<cstring>

    using namespace std;

    int main()

    {
    char str[6];
    char stcp[6];// to copy charachters from stack
    char ch;
    char ch2;// pop(ch2);


    stack s1;//new OBJ of stack
    cin.get(str,6);
    int i=0;
    while (str[i]!=NULL)
    {
    ch=str[i];
    s1.push(ch);
    i++;
    }


    for(int i=0;i<5;i++)
    {
        s1.pop(ch2);
        stcp[i]=ch2;
    }


    int j=0;
    bool ok=false;

    while(stcp[j]!=NULL)
    {
    if(stcp[j]==str[j])
        ok=true;
    else
        {
            ok=false;
            break;
    }
    j++;
    }

    if(ok)
        cout<<"palindrome"<<endl;
    else
        cout<<"Not palindrome"<<endl;



    system("pause");
    return 0;

    }


     i think my proplem in char variable .....
2
Contributors
1
Reply
10
Views
5 Years
Discussion Span
Last Post by Lucaci Andrew
0

Perhaps this function will suite you better in checking if a string is a palindrome.

bool pal(string str){
    Stack<char> s, s2; //we'll use 2 stacks
    for (size_t i=0;i<str.size();i++)
        s.push(str[i]); //put all the characters into the stack
    for (int i=0;i<(int)str.size()/2;i++)
        s2.push(s.pop()); //pop as many elements as string's length/2 and push them into the 2nd stack
    if (str.size()%2==1) s.pop(); //if the size is an odd number, ex: 12321, we'll pop 
                                  //the middle element, we don't need it in our checking.
    while(!s.isEmpty())
        if (s.pop()!=s2.pop()) //verify if the elements are the same
            return false;
    return true;
}

Also, it runs on a Stack class that I have roughly implemented on a simple array:

template <class T>
class Stack{
    T* array;
    int current;
public:
    Stack(){
        array=new T[100];
        current=0;
    }

    bool isEmpty(){ return current==0;}

    T pop(){ if (!isEmpty()) return array[--current];}

    void push(T elem){ array[current++]=elem;}

    ~Stack(){
        delete [] array;
    }
};

Here's how that function works:
string a="12345654321"
In your stack it's represented as:

     |1|
     |2|
     |3|
     |4|
     |5|
     |6|
     |5|
     |4|
     |3|
     |2|
     |1|
      s

What the function will do is to copy strings length/2 elements into the second stack, and your stacks will be like this:

//size of the string is 11, string's length/2 is 5 so     

//   |1| ->  |5|
//   |2| ->  |4|
//   |3| ->  |3|
//   |4| ->  |2|
//   |5| ->  |1|
     |6|     s2
     |5|
     |4|
     |3|
     |2|
     |1|
      s

So now stack s looks like this:

    |6|  
    |5|
    |4|
    |3|
    |2|
    |1|
     s

Having the string's length an odd number, we'll pop the top of the stack, because we don't need that item in order to check if a string is a palindrome (only for odd lenghted strings).
So our strings will look like this, and than we'll compare the elements:

        |5| ?=  |5|
        |4| ?=  |4|
        |3| ?=  |3|
        |2| ?=  |2|
        |1| ?=  |1|
         s      s2

Note that s and s2 have the same number of elements. So, if the checking is done correctly than the string is palindrome, otherwise it's not.

Edited by Lucaci Andrew

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.