   help with finding spaces?
  I am testing inputed words on if they are the same backwards as forwards..but I also want to implement space removal, which I thought would be simple, I cant seem to get .find(' ') to return the positon!any help would be appreciated!use in main:word=clearIt(word);space removal function:string clearIt(string sub){int del;do{del=sub.find(' ');//this is giving a value of -1//even with spaces obviously in the word..//but it finds letters just fine...sub.erase(del,1);}while (del!=-1);return sub;}
  What language is this? It looks like Java but you can never be sure...   ;  c++, sorry forgot to specify   ;  Instead of doing string operations to remove spaces from input in order to check for palindromes, why not have two indexes, one at the end of the string and one at the start of the string, comparing them as they march toward the center whilst skipping invalid characters independently?  (This sounds like homework, so I'll refrain from posting an example for now.)   ;  that would eliminate the need for the spaces removal function I see that, but im more interested in why the find function isnt working for me lol   ;  What's happening is that the cycle runs one loop more than it should. This is actually a common side effect of using a do..while loop, so I recommend avoiding it whenever possible. Also, don't expect std::string::find to return -1 on failure, compare to std::string::npos instead (it may or may not be -1 on all implementations).std::string clearIt(std::string sub) {for (int del = sub.find(' '); del != std::string::npos; del = sub.find(' ')) {sub.erase(del, 1);}return sub;};Quote:Original post by torifor (int del = sub.find(' '); del != std::string::npos; del = sub.find(' ')) {}Mmm, O(n^2) when an O(1) would do.for (int del = sub.find(' '); del != std::string::npos; del = sub.find(' ', del)) {};Quote:Original post by Bregma... when an O(1) would do.Looks like O(n) to me.And yeah, it's not optimal, I was just trying to translate OP's code without altering the algorithm.;  When you say it's "giving a value of -1 even with spaces obviously in the word," are you std::cin >> input_line; or std::getline( std::cin, input_line, '\n'); ?