Like Tree1Likes

is_palindrome

This is a discussion on is_palindrome within the C++ Programming forums, part of the General Programming Boards category; I have written this program with the intention of identifiying palindrome words. Can someone spot why I am running into ...

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

    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;
    }
    In C++14 you just write "auto auto(auto auto) { auto; }".
    The compiler infers the rest from context.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,032
    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
    110
    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;
    }
    In C++14 you just write "auto auto(auto auto) { auto; }".
    The compiler infers the rest from context.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    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.
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

  7. #7
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    110
    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.
    In C++14 you just write "auto auto(auto auto) { auto; }".
    The compiler infers the rest from context.

Popular pages Recent additions subscribe to a feed

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