Thread: Palindromes

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    46

    Post Palindromes

    hey guys i just have a question, if the user of these palindrome programs would put in a bunch of words, how would i make the program output just the palindromes, instead of it stating whether it is a palindrome or not?

    also my professor mentioned to me that I should make two functions with the prototypes:

    std::string reverse(const std::string& s);
    bool is_palindrome(const std::string& s);

    and that would make it easier to check whether a string is a palindrome...

    basically i have to write out the functions...and then output the palindromes from a given input..im pretty sure i could figure out the bool function, but im not sure about the reverse.. also another problem is, im kind of a noob and im not sure exactly how to include the functions in the main function..can somebody just give me like a outline or a step by step or somethin just to show me the right way to do this? thanks a lot..

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Moved from the previously resurrected thread Palindrome emordnilaP.

    Quote Originally Posted by jewelz
    im pretty sure i could figure out the bool function, but im not sure about the reverse
    The underlying idea is the same. What do you have in mind for is_palindrome()?
    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

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    ^thanks..but can u explain that a little more..im kind of lost..

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I very quickly edited my post. You might want to read my updated post.
    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

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    im not sure about the reverse..
    O_o

    The iterator based design of the C++ standard library containers is unparalleled.

    You can, literally, write the 'std::string reverse(const std::string &)' function in a single line.

    Look at your choice of references for the iterator based methods and constructors of 'std::string'.

    Show us some diligence and you'll get some more help.

    Soma

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    alright so..im required to have a header file containing the prototypes and a pal.cc file which defines the two functions ..and as for the bool function i think this would work..i had somebody look over it and he said it looks alright..wat do u guys think?

    Code:
    bool IsPalindrome(const std::string& s)
    {
      int first = 0;
      int last = static_cast<int>(s.size()) - 1;
    
      while(first < last)
      {
        if(s[first] != s[last]) 
        return false;
        
        first++;
        last--;
      }
    
      return true;
    }
    so this code would go into the pal.cc right? im not exactly sure what to include in the header file besides the prototype (do i have to declare some things in the beginning?)
    basically after i make the pal.cc i need to create another file.cc which would call the main function im assuming, and that should allow me to take in some input, and print out all the palindromes..

    so now i need to figure out the reverse function..

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    anyone?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What progress have you made so far?
    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

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    well in my previous post i tried out the bool function, im pretty sure it works, but i also i have some other questions on there regarding the organization of things.. (header file, pal.cc <---includes definitions of functions, and another (filename).cc <----includes the actual main function im assuming, which would allow for me to output all the palindromes..

    so i have to work on the reverse function, and can u guys just help me figure out how to organize all this, such as what do i put in the header file, and how do i start off the pal.cc? thanks a lot..

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    What have you done regarding my statements?

    What have you tried with the 'reverse' function?

    Soma

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    would this work for the string reverse?

    Code:
    std::string reverse(const std::string& s);
    
    {
    
    for (i = s.length()-1;  i>=0; i--)
    return  (s[i]);
    
    }

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by jewelz View Post
    would this work for the string reverse?
    No

    s[i] is a char

    so you return a last char instead of the string
    your loop will not run at all

    and if the length is 0 - your function will not have a return at all
    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

  13. #13
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    no, that will return s[s.length()-1]

    you are missing some basic concepts about functions and variables. you should read up on those a bit.

  14. #14
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    hmm..can somebody help me out with this? i'm really not sure what to do..

  15. #15
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    the algorithm in your last function has the right idea, but you're missing some more fundamental concepts like return statements and data types.

    i should clarify - the for loop in your function has generally the right idea for reversing a string - start at the end and work your way to the beginning. however, that is not to say that is the most efficient or easiest way to go about solving your problem.

    if you show me a function that displays how to use std::strings iterators, i'll help connect the dots for you on how to use them to solve your problem.


    here, look at this:

    http://www.cplusplus.com/reference/string/string/
    Last edited by m37h0d; 02-16-2009 at 12:11 PM.

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