Palindromes

This is a discussion on Palindromes within the C++ Programming forums, part of the General Programming Boards category; ah darn it...using the STL reverse algorithm would be the easiest way, but i'm not allowed to use it..owned...

  1. #16
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    ah darn it...using the STL reverse algorithm would be the easiest way, but i'm not allowed to use it..owned

  2. #17
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    the reversing algorithm is trivial. you have that part more-or-less correct, though it can be done more easily

    std::string::iterator is a part of std::string; you appear to be allowed to use std::string.

  3. #18
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    using that site, i came up with this...i really gotta figure this out...sigh..

    Code:
    string s;
    string::reverse_iterator rit;
      for ( rit=s.rbegin() ; rit < s.rend(); rit++ )
        return rit;

  4. #19
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    jewelz, you're still plagued by a basic misconception about how functions work.

    a function only returns at most ONE value each time it is called. a function with a return type specification of void never returns a value.

    a return statement immediately stops execution of the function and returns the variable specified by the argument of the return statement.

    so, in your pseudofunction above, the for loop accomplishes nothing, because the function will return rit on the first iteration, which is exactly equivalent to:

    Code:
    return s.rbegin();

    the basic anatomy of a function is as follows:

    Code:
    // a free function (i.e. a function that is not a member function of a class)
    returnType functionName(/*argument list here*/)
    {
    // function body
    return instance_of_returnType;   
    }
    
    // a class-member function
    returnType className::functionName(/*argument list here*/)
    {
    // function body 
    return instance_of_returnType;
    }
    you would use a function like this:

    Code:
    #include <vector>
    std::vector<int> countTo(unsigned int number)
    {
         std::vector<int>countList;
         for(unsigned int i=0;i<number;i++)
         {
              countList.push_back(i+1);
         }    
          return countList;
    }
    
    int main(int argc, char* argv[])
    {
          std::vector<int> oneToTen = countTo(10);
    }
    note how the vector is populated over the course of the function execution and then ONE result is returned.
    Last edited by m37h0d; 02-16-2009 at 06:08 PM.

  5. #20
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    this is so frustrating...so basically i have to return the reversed string right? how would i do that..i cant just "return s;" that makes no sense i think..or should i create another string and then += that to the old string? im sorry im such a noob..but i really appreciate the help..

  6. #21
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    Quote Originally Posted by jewelz View Post
    or should i create another string and then += that to the old string?
    SOOoooo close.

    you wouldn't += onto the "old string", but you would += elements of the "old string" onto the new string. i think that's what you meant, but in programming these semantics are very important

    you could modify the string passed into the function argument, except that int your suggested prototype it is passed by const reference. that means you are not allowed to modify it.

    Quote Originally Posted by jewelz View Post
    im sorry im such a noob..but i really appreciate the help..
    don't sweat it. you read a documentation page. that's a victory in and of itself imo. RTFM is job 1

  7. #22
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    ok so creating the new string i would just declare:

    string s2;

    and then im not sure, how do i include this in the for loop?

  8. #23
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    final try for tonight...what do you guys think?

    Code:
    string s2 = "";
                for (int i = 1; i < s.length; i++)
                {
                    s2 = s2 + s[s.length - i];
                }
                s2 = s2 + s[0];
                return s2;

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    If you are allowed to use std::string::reverse_iterator, then from original_string.rbegin() to before original_string.rend(), copy to the new string.

    If you must use indices, then copy the original string into a new string, then reverse the old string by swapping the nth character with the nth last character, all the way from the character at position 0 to the middle of the string.
    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

  10. #25
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    ok guys, so now i have to write a main function that will read words from input and print out all the palindromes, each being on a seperate line..

    so first of all my professor said to use the bool palindrome function in terms of the reverse, so i came up with this:

    Code:
    bool Is_Palindrome(const std::string& s)
    {
    
    if (s==reverse(s))
    return(true);
    else
    return(false);
    
    }
    and as for the main function should i do something like:

    Code:
    int main()
    {
    string s;
    cout << "Input some words ";
    getline (cin, s);
    if (if (Is_Palindrome(s)==true)
    cout<< s <<endl;
    
    return 0;
    
    }
    i tried compiling it, but it gave me some really weird error..and its not even pointing at specific lines..

    anyway let me give u guys the format i have for this program maybe it will help:

    i have a header file called pal.h :

    Code:
    #ifndef GUARD_pal_h
    #define GUARD_pal_h
    
    #include <iostream>
    
    std::string reverse(const std::string& s);
    bool is_palindrome(const std::string& s);
    
    #endif
    a file called pal.cc which includes the definitions of the above functions:

    Code:
    #include "pal.h"
    #include <string>
    
    using std::string;
    
    //function declaration
    std::string reverse(const std::string& s)
    {
        string s2 = "";
        for (int i = 1; i < s.length(); i++)
    	{
    	    s2 = s2 + s[s.length() - i];
    	}
        s2 = s2 + s[0];
        return s2;
    }
    
    bool Is_Palindrome(const std::string& s)
    {
    
        if (s==reverse(s))
    	return(true);
        else
    	return(false);
    
    }
    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?
    Last edited by jewelz; 02-21-2009 at 11:22 AM.

  11. #26
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    Code:
    bool Is_Palindrome(const std::string& s)
    {
        return(s==reverse(s));
    }
    this is simpler

  12. #27
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Well, your earlier method, the IsPalindrome function on the first page, is a lot better... Why even both reversing it all? I don't understand why your professor should say something like that... As a bad hint or as yet another silly must?

  13. #28
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    Quote Originally Posted by EVOEx View Post
    Well, your earlier method, the IsPalindrome function on the first page, is a lot better... Why even both reversing it all? I don't understand why your professor should say something like that... As a bad hint or as yet another silly must?
    yeah i understand what you're saying, but the project description says we need both functions -_-
    Last edited by jewelz; 02-21-2009 at 06:24 PM.

  14. #29
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    it's still not compiling properly..how should i fix up my main function?

  15. #30
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    this is the type of error im getting:

    pal.o: In function `reverse(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
    pal.cc: (.text+0x5c): multiple definition of `reverse(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
    proj3a.o: proj3a.cc: (.text+0x5c): first defined here
    pal.o: In function `Is_Palindrome(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
    pal.cc: (.text+0x217): multiple definition of `Is_Palindrome(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
    proj3a.o: proj3a.cc: (.text+0x217): first defined here

Page 2 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, 02:31 PM
  2. palindromes and bool
    By justinc911 in forum C++ Programming
    Replies: 2
    Last Post: 11-26-2003, 06: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, 05:17 PM

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