Thread: Missing

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99

    Missing

    Write a program that uses both a Stack and a Queue to determine if a number of lines in Text File are packed palindromes or not. A packed palindrome is a sequence of characters with the property: If we remove all the characters except the letters (you may assume all capitals), then the resulting string when reversed is the same as it was before reversal.

    Restrictions: You must use the Circular Array Implementation of the Queue. You may choose which implementation you want for the Stack.

    Test program on the following:
    YOUR NAME
    A
    ABA
    ABC
    ABBA
    X?Y!X
    MADAM. I'M ADAM
    A MAN, A PLAN, A CANAL. PANAMA!
    STRAW? NO, TOO STUPID! I PUT SOOT ON WARTS.
    RACE CAR

    Here's my code:
    Code:
    #include <iostream>
    #include<conio.h>
    #include <string>
    using namespace std;
    
    const int DefaultListSize = 50;
    typedef char Elem;
    
    class Astack 
    {
    private:
    	int top;	/*Index for top element*/
    	int size;	/*Maximum size of stack*/
    	Elem*listArray;	/*Array holding stack elements*/
    public:	
    		Astack(int sz =DefaultListSize)	/*Constructor*/
    		{size = sz; top = 0; listArray = new Elem[sz];}
    		~Astack() { delete [] listArray;}	/*Destructor*/
    		void clear() {top = 0;}
    		bool push(const Elem& item){
    			if(top == size) return false;	/* Stack is full*/
    			else {listArray [top++] = item;
    				return true;
    			}
    		}
    		bool pop(Elem& item){	/*Pop top element*/
    			if(top == 0) return false;
    			else {item = listArray[--top]; 
    				return true;
    			}
    		}
    		bool topValue(Elem& item) const {	/*Return top element*/
    			if (top == 0) return false;
    			else {item = listArray[top - 1];
    			return true;
    			}
    		}
    		int length() const {return top;}
    		bool IsEmpty() const {if(top == 0) return true;
    		else return false;
    		}
    	};
    
    class AQueue
    {
    private:
    	int size;	/*Maximum size of queue*/
    	int front;	/*Index of front element*/
    	int rear;	/*Index of rear element*/
    	Elem*listArray;	/*Array holding queue elements*/
    public:
    	AQueue(int sz =DefaultListSize){	/*Constructor*/
    		size = sz + 1;
    		rear = 0;
    		front = 1;
    		listArray = new Elem[size];
    	}
    	~AQueue() {delete [] listArray; }	/*Destructor*/
    	void clear() {front = rear; }
    	bool enqueue(const Elem& it) {
    		if(((rear+2) % size) == front) return false; /*Full*/
    		rear = (rear+1) % size;	/*Circular increment*/
    		listArray[rear] = it;
    		return true;
    	}
    	bool dequeue(Elem& it) {
    		if (length() == 0) return false;	/*Empty*/
    		it = listArray[front];
    		front = (front+1) % size;	/*Circular increment*/
    		return true;
    	}
    	bool frontValue(Elem& it) const {
    		if (length() == 0) return false; /*Empty*/
    		it = listArray[front];
    		return true;
    	}
    	virtual int length() const
    	{ return ((rear+size) - front+1) % size; }
    };
    
    int main()
    {
    char str[100];
    bool flag;
    Astack S;
    AQueue Q;
    
    cout<<"Enter word :";
    cin>>str;
    
    int x=strlen(str)-1;
    for(int i=0;i<=x;i++)
    {
    if(str[i]==str[x-i])
    {
    flag=true;
    continue;
    }
    else
    {
    flag=false;
    break;
    }
    }
    if(flag==true)
    cout<<"Palindrome"<<endl;
    else
    cout<<"Not a palindrome"<<endl;
    getch();
    }
    I need to figure where to push and pop my string through the Stack and Queue. And I need to figure out how to ignore the anything that is not a letter.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I'm curious as to why your class is nicely indented, and your main is basically dog food.
    SourceForge.net: Indentation - cpwiki

    Also, choose whether this is C or C++.
    If it's C++, then you should really be using std::string in main, not a char array and strlen.

    Finally, look up ctype.h (or even cctype if you want to go full C++)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    I didnt finish organizing my code. I want to use C++
    Code:
    while(str[i]){
    	if(ispunct(str[i]))
    		cin.ignore();
    		i++;
    }
    Will this ignore any punctuations I might have in my string? If so how should I define i?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Will this ignore any punctuations I might have in my string?
    Did you read the manual page for ispunct()

    Does it find ALL the characters you're not interested in?

    Maybe isalpha() would be a better test (for inclusion)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what am I missing? (Program won't compile)
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 08-25-2009, 03:01 PM
  2. LDAP Query
    By Travoiz in forum C++ Programming
    Replies: 0
    Last Post: 08-13-2009, 02:58 PM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM