Palindromes

This is a discussion on Palindromes within the C++ Programming forums, part of the General Programming Boards category; What is your current code? It looks like you are defining reverse twice, or there is a conflict with the ...

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    What is your current code? It looks like you are defining reverse twice, or there is a conflict with the standard library version.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,303
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,303
    Yes.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you do not need to store all the strings to find the longests

    enogh to have 2 strings - longest_so_far and current
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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,185
    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
    839
    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
    839
    that looks good

  14. #44
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    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)

Page 3 of 4 FirstFirst 1234 LastLast
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, 03: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, 06:08 PM
  4. Palindromes
    By cman in forum C Programming
    Replies: 1
    Last Post: 05-05-2003, 07:39 AM
  5. strings and palindrome's
    By watshamacalit in forum C Programming
    Replies: 6
    Last Post: 01-07-2003, 06:17 PM

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