Need help , Palindrome program

This is a discussion on Need help , Palindrome program within the C++ Programming forums, part of the General Programming Boards category; Hi, i have this program that i made but my code really stinks not cause the sintaxis code but the ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    12

    Need help , Palindrome program

    Hi, i have this program that i made but my code really stinks not cause the sintaxis code but the functionality is ugly, look the program recive a string like this madamxmadam an then start to push the data into S so when find a x start to compare the pop() function in s with the rest of the string x but... i have problems for example if i put "exee" the programm doesnt do anything or if i put madamxmadam i have a this messega like the message on windows xp "temp.exe has encountered a problem and needs to close. We are sorry for the inconvenience." the program runs but before the message pres any key to continue appears then the error comes...

    i really apreciate if you have a better idea for this code... like i dont made a anthor string a copy... or idont.... please i really need help...
    Code:
    #include <iostream.h>
    #include<string.h>
    
    template <class T>
    class stack
    {
    	private:
    		T *S;
    		int length;
    		int top;
    	public:
    		stack (int inilength);
    		~stack();
    		palx(T *x);
    		int empty();
    		push(T x);
    		T pop();
    };
    
    template <class T>
    stack<T>::stack(int inilength)
    {
    	length=inilength;
    	S=new T[length];
    	top=-1;
    }
    
    template <class T>
    stack<T>::~stack()
    {
    	delete S;
    }
    
    template <class T>
    int stack<T>::empty()
    {
    	if(top==-1)
    	{ return 1; }
    	else return 0;
    }
    
    template <class T>
    stack<T>::push(T x)
    {
    	top++;
    	S[top]=x;
    }
    
    template <class T>
    T stack<T>::pop()
    {
    	
    	if (empty()==-1)
    	{ cout<<"Error Underflow"; }
    	else 
    	{
    			top--;
    	return S[top+1];
    	}
    
    }
    
    template <class T>
    stack<T>::palx(T *x)
    {
    	int j=0;
    	int str=strlen(x);
    	char comp;
    
    	if(j<str)
    	{
    		while(x[j]!='x')
    		{
    			push(x[j]);
    			j++;
    		}
    		
    		if(x[j]=='x')
    		{
    			while(top>-1)
    			{
    				j++;
    				comp=pop();
    				if(comp!=x[j])
    					{cout<<"No is not";top=-1;}
    				else if((j==str-1) && (top<0))
    				{ cout<< "yes it is"; }
    			}
    		}
    	}
    }
    
    
    void main()
    {
    	
    	char *palindrome;
    	palindrome = new char(50);
    	stack<char> *nueva;
    	nueva = new stack<char>(50);
    	cout<<"Dame un disque palindrome: "<<endl;
    	cin>>palindrome;
    	nueva->palx(palindrome);
    	delete nueva;
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
                    while(x[j]!='x')
    		{
    			push(x[j]);
    			j++;
    		}
    		
    		if(x[j]=='x')
    		{
    The if loop is always true, since x[j]=='x' is the only condition that ends the while loop.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Banned
    Join Date
    Jun 2005
    Posts
    594
    i dont know what compiler you are using but

    #include <iostream> // not iostream.h
    #include <string> //not string.h

    you might want to add using namespace std;
    but some people aroudn here are starting to frown on that.

    and main should be int main()
    and should return 0; for good measure.

    Those are some glarring thing that i just
    noticed i cant compile your code cause i need to leave
    for work. But maybe that will help reduce and error
    your getting.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    { cout<< "yes it is"; }
    Maybe you need a break in there?

    Also, void main() should be int main().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    template <class T>
    stack<T>::palx(T *x)
    {
    	int j=0;
    	int str=strlen(x);
    You're using strlen() on T, which could be a class. Therefore, you're assuming T is char * . . . why not just make it so?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    One last thing . . .
    Code:
            int j=0;
    	int str=strlen(x);
    	char comp;
    
    	if(j<str)
    The if(j<str) could just be if(0<str) or if(str>0).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> #include <string> //not string.h
    No, <string.h> is correct in this instance, although it is not needed because no string manipulation functions are used.

    >> palindrome = new char(50);
    should be palindrome = new char[50]; and add delete [] palindrome; to the end of main.

    With those changes and changing to <iostream> (my compiler doesn't support the non-standard <iostream.h>), your program worked on madamxmadam.

  8. #8
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
    i really apreciate if you have a better idea for this code... like i dont made a anthor string a copy... or idont.... please i really need help...
    Of course, there are much simpler ways to implement a palindrome function which avoids the need to use the stack and constructors or destructors, however, it would appear teachers explicitly ask for students to use these as means to demonstrate their functionality.

    If you do a quick search of the existing posts you should find something useful.

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    
    int IsMirrorPalindrome( const char* word, size_t length )
    {
       if( length <= 1) return 1;
       else
       return ( (word[0] == word[length-1]) && IsMirrorPalindrome(word+1,length-2));
    }
    
    int IsPalindrome(std::string test)
    {
       std::string tempcopy;
       std::string::iterator iter = test.begin();
       const std::string::iterator end = test.end();
       while ( iter != end )
       {
          const char c = *iter;
          if ( (!std::ispunct(c)) && (!std::isspace(c)) ) tempcopy.push_back(std::tolower(c));
          ++iter;
       }
    
       if (IsMirrorPalindrome(tempcopy.c_str(),tempcopy.leng  th()))
          return 1;
       else
          return 0;
    }
    
    
    int main()
    {
       std::string test;
       std::cout<<" enter string.... ";
       std::getline(std::cin,test);
       if (IsPalindrome(test))
          std::cout<<std::endl<<"palindrome"<<std::endl;
       else
          std::cout<<std::endl<<" not palindrome"<<std::endl;
       getchar();
    }
    Was what i came up with for the last palindrome question we answered.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Code:
    template <class T>
    stack<T>::push(T x)
    {
        top++;
        S[top]=x;
    }
    You never check if you are accessing an out of bounds element.

    [edit]If you are dead set on using a stack to solve this, might I suggest using the STL stack container already available in the <stack> header in order to test your overall logic before you get to implementing your own stack. You might not even need to use your own stack implementation which would be one less thing to need to debug (unless the purpose of this program is not really focussed on solving the palindrome question but actually testing your own stack implementation).

    The quickest/shortest way I've ever come up with to solve the question as to whether or not a string is a palindrome is to use the equal function in the <algorithm> header and use forward and reverse iterators. Then the whole test is just a single line of code without you needing anything more than 5 or 6 extra lines of code for a variable declaration for the string to test and a prompt for input followed by an input line and an output line or two for the "yes it is" or "no it isn't".[/edit]
    Last edited by hk_mp5kpdw; 07-11-2005 at 12:22 PM.
    "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. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Palindrome Program using Memory mapping
    By rrsanch in forum C++ Programming
    Replies: 1
    Last Post: 09-24-2004, 11:49 PM
  4. Palindrome program help please
    By hunter_04 in forum C++ Programming
    Replies: 7
    Last Post: 09-20-2004, 07:38 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21