Palindromes

This is a discussion on Palindromes within the C++ Programming forums, part of the General Programming Boards category; hey guys i just have a question, if the user of these palindrome programs would put in a bunch of ...

  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
    22,302
    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()?
    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

  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
    22,302
    I very quickly edited my post. You might want to read my updated post.
    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

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,415
    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
    22,302
    What progress have you made so far?
    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

  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
    4,415
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

Page 1 of 4 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