Thread: Palindrome city

  1. #16
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    that would work, but I need to use the class stack and it's functions in the program.

  2. #17
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    That actually helped me. But it is messing up on me, giving me an extra "not palindrome" at the end. It's 3 in the morning here and I keep staring at the screen getting evenmore confused....

  3. #18
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Code:
    int main()
    {
        ifstream infile;
        ofstream outfile;	
        stack One, Two, Three;  //defines stacks
        char x,temp;
        infile.open("palindrome.txt");//open file to read from 
        if( !infile.good() )
        {
            cout << "Error opening file" << endl;
            system("PAUSE");
            exit(1);
        }
    	
        while( infile.get(x) )
        {
            x=toupper(x);                //converts characters to upper case
            cout << x << '\t';	  	
            if( (ispunct(x)==0) && (x != ' '))
            {
    	One.push(x);
    	Two.push(x);
            }	
        }
    
        cout << endl;
    	
        while ( !One.isEmpty() )
        {
            Three.push ( One.pop() );
        }
    	
        bool success=true;
        while ( !Two.isEmpty() && success)
        {
            if (Two.pop() != Three.pop() )
    	success = false;
        }
    	
        if (success)
             cout << "word is a palindrome" << endl;
        else
             cout << "Word is NOT a palindrome" << endl;
    	
        system("PAUSE");
        return 0;
    }

  4. #19
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    Thanks, but that only seems to evaluate one word or character

  5. #20
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    I changed it to do that. I have included the text file that I am reading from.

    This is what I have so far:

    Code:
    #include <iostream.h>
    #include <string.h>
    #include <fstream.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    class stack
    {
    public:
    	struct node;
    	typedef node* nodeptr;
    	struct node
    	{ char c;
    	nodeptr next;
    	};
    	stack();
    	void makeStackEmpty();//deletes stack
    	void push(char);//puts item on top of stack
    	char pop();//takes off item on top and brings back what was there
    	bool isEmpty();//is stack empty
    	void print();//debugging
    	private:
    		nodeptr top;
    };
    
    stack::stack()
    {top=0;
    }
    
    void stack::makeStackEmpty()//deletes stack
    {
    	nodeptr temp;
    	temp=top;
    	while(temp!=0)
    	{
    		top=top->next;
    		delete temp;
    		temp=top;
    	}
    }
    
    void stack::push(char x)//puts item on top of stack
    {
    	nodeptr temp;
    	temp=new node;
    	temp->c=x;
    	temp->next=top;
    	top=temp;
    }
    
    char stack::pop()//takes off item on top and brings back what was there
    {
    	nodeptr temp;
    	char x;
    	if(isEmpty())
    		cout << "error -- cannot pop from an empty stack\n";
    	else
    	{
    		temp=top;
    		x=top->c;
    		top=top->next;
    		delete temp;
    	}
    	return x;
    }
    
    bool stack::isEmpty()//is stack empty
    {
    	if(top==0)
    		return true;
    	else
    		return false;
    }
    
    void stack::print()//prints
    {
    	stack t;
    	char w;
    	while(!isEmpty())
    	{
    		w=pop();
    		cout << w << " ";
    		t.push(w);
    	}
    	while(!t.isEmpty())
    	{
    		w=t.pop();
    	}
    	cout << endl;
    }
    //Globals
    ifstream infile;
    ofstream outfile;
    
    typedef char string[200];
    
    //---------------------------------------------------------------------------//
    int main()
    {
    	int i=0;
    	stack One, Two, Three;  //defines stacks
    string x,temp;
    infile.open("Pals1.txt", ios::nocreate);//open file to read from 
    	if(!infile)
    	{
    		cout << "Error opening file" << endl;
    		exit(1);
    	}
    	outfile.open("palindrome");
    
    while(infile)
    {
    	i++;
    	infile.get(x[i]);               
    cout << x[i];
    
    	x[i]=toupper(x[i]); 
    
    
    //--------------------------------------------------------------------C++ Board
    
    
    
    if(ispunct(x[i])==0&&x[i]!='\n')    
    {
      One.push(x[i]);
      Two.push(x[i]);
      
    }
    else
    {
      while ( !Two.isEmpty() ) {
        Three.push( Two.pop() );
    
      }
      // Three is now the reverse of Two (and One)
    
      // compare each char with its reversed counterpart
      
      
      
      
      bool isPal = true;
      while ( !One.isEmpty() ) {
        if ( One.pop() != Three.pop() ) {
          isPal = false;
        }
      }
    
      if ( isPal )
      {
        cout << "palindrome" << endl;
      }
      else
      {
        cout << "not palindrome" << endl;
      }
    }
    
    
    
    
    
    
    
    //---------------------------------------------------------------------/C++ Board
    
    
    
    
    	/*if(ispunct(x[i])==0&&x[i]!=' ')    
    	{
    		
        s1.push( x[i] );
        s2.push( x[i] );
    
    while (s1.isEmpty())
    {
    	s3.push( s1.pop( ) );
    }
    
    while (s2.isEmpty( ))
    {
    	if( s2.pop() != s3.pop() )
            cout << "...not a palindrome..." << endl;
    else
    		cout << "...is a palindrome..." << endl;
    	}
    		*//*One.push(x[i]);
    		Two.push(x[i]);
    		Three.makeStackEmpty();
    		//temp[i]=Two.pop();
    		Three.push(Two.pop());
    		Three.print();
    		*/
    		/*if()
    		{
    		cout << "is a palindrome" << endl;
    	}*/
    	
    
    }
    	return 0;
    
    }
    //--------------------------------------------------------------------------//

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. Big help in Astar search code...
    By alvifarooq in forum C++ Programming
    Replies: 6
    Last Post: 09-24-2004, 11:38 AM
  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