Thread: Palindrome

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

    Palindrome

    My code builds but it doesnt do anything. I think I have two things wrong with my code and Im stuck on how to fix these. I think the first one is I may not be reading my string as a char correctly and my second one I think is that Im might not have used ch2 correctly in oder for me to dequeue into it.

    Here's my code:
    Code:
    #include<iostream>
    #include<string>
    #include<ctype.h>
    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()
    {
    	Astack S;
    	AQueue Q;
    	string p;
    	char ch1;
    	char ch2;
    	unsigned int i;
    
    	cout << "Enter what you think is a Palindrome\n";
    	getline(cin, p);
    
    	for(i = 0; i < p.length(); i++){
    		while((ch1 = cin.get()) != EOF){
    			if(ch1 != '\0')
    				cout << ch1;
    
    			if(('A' <= ch1) && (ch1 <= 'Z')){
    				S.push(ch1);
    				Q.enqueue(ch1);
    			}
    		}
    	}
    
    	while(!S.IsEmpty()){
    		S.pop(ch1);
    		Q.dequeue(ch2);
    			cout << ch1;
    	}
    	if(ch1 == ch2)
    		cout << "It is a Packed Palindrome" << endl;
    	else
    		cout << "It is not a Packed Palindrome" << endl;
    	return 0;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You need to decide where you're reading from. In one place, you read your test sentence into a std::string object. Next, you try to read using cin.get to again read some data. Why? If you've already got the sentence you wish to test in the string object why have another loop that attempts to read in some more data. It seems that you'd want to process what you already have in the std::string.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error in Recursive String Palindrome Code
    By clegs in forum C Programming
    Replies: 13
    Last Post: 12-21-2008, 12:36 PM
  2. Is it a Palindrome?
    By xp5 in forum C Programming
    Replies: 3
    Last Post: 09-06-2007, 05:26 AM
  3. bool palindrome definition
    By justinc911 in forum C++ Programming
    Replies: 3
    Last Post: 11-26-2003, 05:50 PM
  4. Palindrome Coding trouble
    By TheLoneWolf32 in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 07:05 PM
  5. palindrome HELP !!!
    By TED22_8 in forum C Programming
    Replies: 23
    Last Post: 01-22-2002, 02:14 PM