Thread: Palindromes

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code? It looks like you are defining reverse twice, or there is a conflict with the standard library version.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #32
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    everything in my last post is my current code...i didn't change anything from that..

  3. #33
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, I see the problem now:
    Quote Originally Posted by jewelz
    and as for the main function i posted above i included "pal.h" and "pal.cc" and then the appropriate declarations and usings..what might be the problem?
    You should not be including "pal.cc". You should compile pal.cc separately and link it with the object file produced by compiling main.cc (or whatever source file the main function is in).

    Instead of including <iostream> in pal.h, include <string> since you refer to std::string in pal.h.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #34
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    Quote Originally Posted by laserlight View Post
    Oh, I see the problem now:

    You should not be including "pal.cc". You should compile pal.cc separately and link it with the object file produced by compiling main.cc (or whatever source file the main function is in).

    Instead of including <iostream> in pal.h, include <string> since you refer to std::string in pal.h.
    so i dont have to include "pal.cc" anywhere?

  5. #35
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #36
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    ok well this is what i came up with for printing the the palindromes:

    Code:
    int main()
    {
    
        string s;
        
        while(cin >> s){
    	if(isPalindrome(s)== true)
    	    cout << s << endl;
    
        }
    return 0;
    
    }
    now for the last part, the program will take input and output the longest palindrome, along with its length..i came up with this:

    Code:
    int main()
    {
    
        string s;
        vector<string> words;
        
        while(cin >> s) && (isPalindrome(s)== true)
    			{
    			    words.push_back(s);
    			}
        string longest = words[0];
    
        for (int i = 1; i != words.size(); i++) {
    	if(words[i].length() > longest.length()) {
    	    longest = words[i];
    	}
    
    	cout <<"Longest Palindrome is " << "\"" << longest << "\""
    	     << "whose length is " << longest.length() << endl;
    
        
    	return 0;
    
        }
    does this seem ok?
    Last edited by jewelz; 02-22-2009 at 02:35 PM.

  7. #37
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you do not need to store all the strings to find the longests

    enogh to have 2 strings - longest_so_far and current
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #38
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    um..can u explain that a little more please? my method wouldn't output the longest palindrome?
    Last edited by jewelz; 02-22-2009 at 03:53 PM.

  9. #39
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It would, eventually. But you don't need to store all the words, just the longest (unless you need to keep all the palindromes for other reasons).

  10. #40
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    ok is this better??

    Code:
    int main()
    
    {
    typedef string:: size_type string _size;
    
    string s;
    string_size max = s.size();
    string longest = s;
        
        while(cin >> s) && (isPalindrome(s)== true) {
    	string_size size = s.size();
                if (size > max) {
                   max = size;
                  longest = s;
            }
    
    }
       
       cout <<"Longest Palindrome is " << "\"" << longest << "\""
    	     << "whose length is " << max << endl;
    
        
    	return 0;
    
    }

  11. #41
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    you've got erroneous spaces in your typedef.

  12. #42
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    Code:
    typedef string::size_type string_size;
    is everything good now?? =)
    Last edited by jewelz; 02-22-2009 at 04:37 PM.

  13. #43
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    that looks good

  14. #44
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    incidentally,i think it's very odd you weren't encouraged to do something like this:

    Code:
    bool isPalindrome(const std::string& s)
    {
         std::string::const_iterator fit = s.begin();
         std::string::const_iterator rit = s.end();
         --rit;
         for(unsigned int i=0;i<s.size()/2;i++,fit++,rit--)
         {
            if(*fit==' ')
                ++fit;
            if(*rit==' ')
                --rit;
            if(*fit != *rit)
             return false;
         }
         return true;
    }

  15. #45
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    hmm..getting a compilation error..it's saying:

    error: expected identifier before '(' token

    indicating the line with while (cin >> s)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Palindromes and Bitwise operators
    By Dr Tornillo in forum C Programming
    Replies: 8
    Last Post: 08-02-2007, 02:31 PM
  2. palindromes and bool
    By justinc911 in forum C++ Programming
    Replies: 2
    Last Post: 11-26-2003, 07:58 PM
  3. palindromes....
    By imbecile in C in forum C Programming
    Replies: 8
    Last Post: 08-09-2003, 05:08 PM
  4. Palindromes
    By cman in forum C Programming
    Replies: 1
    Last Post: 05-05-2003, 06:39 AM
  5. strings and palindrome's
    By watshamacalit in forum C Programming
    Replies: 6
    Last Post: 01-07-2003, 06:17 PM