title: Difference between these two functions that find Palindromes
body: <p>I wrote a function to check whether a word is palindrome or not but "unexpectedly", that function failed quite badly, here it is:</p>
  
  <pre><code>bool isPalindrome (const string&amp; s){
   string reverse = "";
   string original = s;
   for (string_sz i = 0; i != original.size(); ++i){
    reverse += original.back();
    original.pop_back();
   }
  
   if (reverse == original)
    return true;
   else
    return false;
  }
  </code></pre>
  
  <p>It gives me "string iterator offset out of range error" when you pass in a string with only one character and returns true even if we pass in an empty string (although I know its because of the intialisation of the reverse variable) and also when you pass in an unassigned string for example:</p>
  
  <pre><code>string input;
  isPalindrome(input);
  </code></pre>
  
  <p>Later, I found a better function which works as you would expect:</p>
  
  <pre><code>bool found(const string&amp; s)
  {
   bool found = true;
   for (string::const_iterator i = s.begin(), j = s.end() - 1; i &lt; j; ++i, --j) {
    if (*i != *j)
     found = false;
   }
   return found;
  }
  </code></pre>
  
  <p>Unlike the first function, this function correctly fails when you give it an unassigned string variable or an empty string and works for single characters and such...</p>
  
  <p>So, good people of stackoverflow please point out to me why the first function is so bad...</p>
  
  <p>Thank You.</p>