-4

I must take a few words and test to see if they are palindromes, not counting the characters of space, "," and "-".

      #include <iostream>
    #include <algorithm>
    #include <string>
    using namespace std;
    int main() 
    {
       int inputs;
       for (int i=0;i<inputs;i++)
      {string str;
      getline(cin, str);
      transform(str.begin(), str.end(), str.begin(), ::tolower);
       str.erase(remove_if(str.begin(),str.end(),','),str.end());
       str.erase(remove_if(str.begin(),str.end(),'-'),str.end());
       str.erase(remove_if(str.begin(),str.end(),' '),str.end());
      if (str == string(str.rbegin(), str.rend()))
      cout << "Y ";
      else 
      cout << "N "; }

}  

the problems: In file included from /usr/include/c++/4.9/bits/stl_algobase.h:71:0, from /usr/include/c++/4.9/bits/char_traits.h:39, from /usr/include/c++/4.9/ios:40, from /usr/include/c++/4.9/ostream:38, from /usr/include/c++/4.9/iostream:39, from solution.cc:1:/usr/include/c++/4.9/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_pred<_Predicate>::operator()(_Iterator) [with _Iterator = __gnu_cxx::__normal_iterator >; _Predicate = char]':/usr/include/c++/4.9/bits/stl_algo.h:866:28: required from '_ForwardIterator std::__remove_if(_ForwardIterator, _ForwardIterator, _Predicate) [with _ForwardIterator = __gnu_cxx::__normal_iterator >; _Predicate = __gnu_cxx::__ops::_Iter_pred]'/usr/include/c++/4.9/bits/stl_algo.h:937:47: required from '_FIter std::remove_if(_FIter, _FIter, _Predicate) [with _FIter = __gnu_cxx::__normal_iterator >; _Predicate = char]'solution.cc:12:49: required from here/usr/include/c++/4.9/bits/predefined_ops.h:231:30: error: expression cannot be used as a function { return bool(_M_pred(*__it)); } ^

(complete jibberish to me.) Any solutions?

Goodwin Lu
  • 45
  • 1
  • 12
  • Are you testing each word, or a sequence of words. I'm unclear what the boundary is. – Simon Parker Jun 01 '16 at 00:05
  • 1
    If you really wanted to do this in C++ and use algorithms, this is a 5 or 6 line program using `std::reverse` and `std::remove_if`. – PaulMcKenzie Jun 01 '16 at 00:05
  • 1
    [How to debug small programs.](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Baum mit Augen Jun 01 '16 at 00:09
  • *how is it possible that it is always not a palindrome* -- How is it possible that the first input is a palindrome when the first character 'A' is not equal to the last character 'a', and you have nowhere in your code a conversion of letters to the same case (all upper or all lower)? That right there is a signal that you should debug your code. In addition, a whole chunk of code is missing to make the letters the same casing. – PaulMcKenzie Jun 01 '16 at 00:27
  • @SimonParker Based on the correct answer, it must be the entire string excluding non alphanumeric. – Fantastic Mr Fox Jun 01 '16 at 00:28
  • 1
    Reminder: In C++, letters are *case sensitive*. This means 'A' != 'a'. – Thomas Matthews Jun 01 '16 at 00:42

2 Answers2

3

Check for palindrome can be as simple as

#include <iostream>
#include <algorithm>
#include <string>

int main() {

  std::string str, rStr;

  std::cout << "Enter String :\n";
  std::cin >> str;

  std::transform(str.begin(), str.end(), str.begin(), ::tolower);
  rStr = str;
  std::reverse(begin(str), end(str));

  (rStr == str) ? std::cout << "Word is palindrone\n" : std::cout << "Word is not palindrone\n";

  return 0;
}

Modify as you wish.

To get more than one word as input, use getline(cin, str);

hello
  • 990
  • 2
  • 21
  • 49
2

Might as well show a solution that contrasts dramatically to your attempt:

#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

int main() 
{
    std::string orig;
    while (std::getline(std::cin, orig))
    {
        std::transform(orig.begin(), orig.end(), orig.begin(), ::toupper);

        // erase the punctuation and spaces
        orig.erase(std::remove_if(orig.begin(), orig.end(), [](char ch) 
        { return ::isspace(ch) || ::ispunct(ch);}), orig.end()); 

        std::cout << ((orig == std::string(orig.rbegin(), orig.rend()))?"Y":"N");
    }
}

Note that std::reverse was not needed, and the usage of the erase / remove_if idiom to remove the spaces and punctuation.

PaulMcKenzie
  • 31,493
  • 4
  • 19
  • 38
  • 1
    I really want to give you a +1 but [using namespace std is poor practice](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice) – Serdalis Jun 01 '16 at 03:07
  • @Serdalis It is not bad practice if used within a module as small as this one. It is bad practice when it is used in header files, where inclusion of the header will inadvertently bring in stuff you don't want. – PaulMcKenzie Jun 01 '16 at 03:09
  • I have still seen it bite people when they include it at the top of a C++ source file. Mostly with small helper functions and such. Just a pet hate I have and off topic, this answer is still great. ( +1 now ). – Serdalis Jun 01 '16 at 03:15
  • 2
    @Serdalis No problem. I changed the code anyway. – PaulMcKenzie Jun 01 '16 at 03:16
  • what does the question mark do? – Goodwin Lu Jun 01 '16 at 10:51
  • and why am I getting an extra "y" at the beginning with your code? – Goodwin Lu Jun 01 '16 at 11:01
  • @GoodwinLu [This works correctly](http://ideone.com/XnFnFN). Maybe because I left out the unnecessary `16` and just processed the strings? The question mark is the [ternary operator](http://stackoverflow.com/questions/392932/how-do-i-use-the-conditional-operator) – PaulMcKenzie Jun 01 '16 at 13:16
  • @GoodwinLu [Using your original data without the 16](http://ideone.com/O5UC25) – PaulMcKenzie Jun 02 '16 at 01:53
  • I don't understand your question. Just take an editor and erase that input line. – PaulMcKenzie Jun 02 '16 at 21:16