Thread: is_palindrome

  1. #1
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528

    is_palindrome

    I have written this program with the intention of identifiying palindrome words.
    Can someone spot why I am running into a segmentation fault ?
    Code:
    #include <vector>
    #include <iostream>
    #include <string>
    bool is_palindrome ( const std::string & s )
    {
      return equal ( s.begin(),s.end(),s.rbegin() );
    }
    std::istream & read ( std::istream & s , std::string& temp_store )
    {
      if ( s )
         while( s >> temp_store )
          return s;
    }
     std::vector< std::string > check_for_palindrome ( std::istream & s  )
    {
      std::vector < std::string > palindrome_words;
      
       std::string word_read;
       
      while( read ( s , word_read )){
      
        if ( is_palindrome ( word_read ) )
          palindrome_words.push_back(word_read);
      }
      return palindrome_words;
    }  
    int main ( int argc ,char * argv[] )
    { 
      std::vector < std::string > words_reported_palindrome;
      
      words_reported_palindrome = check_for_palindrome ( std:: cin );
      
      typedef std::vector < std::string > ::const_iterator iter;
      
      for ( iter my_iter = words_reported_palindrome.begin(); my_iter != words_reported_palindrome.end(); ++my_iter)
        
      {
        std::cout << *my_iter << std::endl;
        
      }
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Just run it through a debugger and backtrace where it crashes.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    I had that but the problem was how to solve it.
    I rewrote the whole thing.Thanks for taking a look.
    Code:
    #include <vector>
    #include <iostream>
    #include <string>
    bool is_palindrome ( const std::string & s )
    {
      return equal ( s.begin(),s.end(),s.rbegin() );
    }
    std::istream & read ( std::istream & s , std::vector < std::string > &v )
    {
      if ( s ){
       std::string temp_store; 
        while ( s >> temp_store )
          if ( is_palindrome ( temp_store))
       v.push_back(temp_store);
      }
          return s;
    }
     /*std::vector< std::string > check_for_palindrome ( std::istream & s  )
    {
      std::vector < std::string > palindrome_words;
      
       std::string word_read;
       
      while( read ( s , word_read )){
      
        if ( is_palindrome ( word_read ) )
          palindrome_words.push_back(word_read);
      }
      return palindrome_words;
    }  */
    int main ( int argc ,char * argv[] )
    { 
      std::vector < std::string > words_reported_palindrome;
      
      //words_reported_palindrome =check_for_palindrome ( std:: cin );
      read ( std::cin , words_reported_palindrome );
      
      typedef std::vector < std::string > ::const_iterator iter;
      
      for ( iter my_iter = words_reported_palindrome.begin(); my_iter != words_reported_palindrome.end(); ++my_iter)
        
      {
        std::cout << *my_iter << std::endl;
        
      }
      return 0;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you want to use std::equal, you should #include <algorithm>. Once you do that, you might also #include <iterator> then replace this:
    Code:
      typedef std::vector < std::string > ::const_iterator iter;
       
      for ( iter my_iter = words_reported_palindrome.begin(); my_iter != words_reported_palindrome.end(); ++my_iter)
         
      {
        std::cout << *my_iter << std::endl;
         
      }
    with:
    Code:
    std::copy(words_reported_palindrome.begin(),
              words_reported_palindrome.begin(),
              std::ostream_iterator<std::string>(std::cout, "\n"));
    By the way, I suggest that you indent consistently by more than a space. Personally, I prefer four spaces.
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by laserlight View Post
    Code:
    std::copy(words_reported_palindrome.begin(),
              words_reported_palindrome.begin(),
              std::ostream_iterator<std::string>(std::cout, "\n"));
    should not the end() somewhere in this copy?
    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

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, copy and paste error.
    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

  7. #7
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by laserlight View Post
    If you want to use std::equal, you should #include <algorithm>. Once you do that, you might also #include <iterator> then replace this:
    Code:
      typedef std::vector < std::string > ::const_iterator iter;
       
      for ( iter my_iter = words_reported_palindrome.begin(); my_iter != words_reported_palindrome.end(); ++my_iter)
         
      {
        std::cout << *my_iter << std::endl;
         
      }
    with:
    Code:
    std::copy(words_reported_palindrome.begin(),
              words_reported_palindrome.begin(),
              std::ostream_iterator<std::string>(std::cout, "\n"));
    By the way, I suggest that you indent consistently by more than a space. Personally, I prefer four spaces.
    Thanks very much.I am still awed by the power of the library.
    I got a bunch of errors when I replaced "\n" with '\n' (am such idiot , don't chuckle ) but I don't care bring them on.
    Last edited by Aslaville; 07-19-2013 at 03:34 AM.

Popular pages Recent additions subscribe to a feed