fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <iterator>
  5. using namespace std;
  6.  
  7. bool checkPalindrome(const string& str) {
  8. for (auto p = str.begin(), q = str.end(); p!=q && p!=q+1; p++ )
  9. if (*p!=*--q) // if char from front doesn't match char from rear ?
  10. return false; // then it's not a palindrome !
  11. return true;
  12. }
  13.  
  14. int main(){
  15. vector<string> str { "level", "deed", "leaf", "dead","","e","de" };
  16. vector<bool> expected { true, true, false, false, true, true, false };
  17.  
  18. for (size_t i = 0; i< str.size(); i++) {
  19. bool test = checkPalindrome(str[i]);
  20. cout << str[i] << " would " << (test?"":"not ") << "be a palindrome: "
  21. << (test==expected[i]?"correct!":"FAILURE!!!")<<endl;
  22. }
  23.  
  24. char *s="blaalb";
  25. cout << checkPalindrome(s); // no problem ! gets converted
  26. return 0;
  27. }
  28.  
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
level would be a palindrome: correct!
deed would be a palindrome: correct!
leaf would not be a palindrome: correct!
dead would not be a palindrome: correct!
 would be a palindrome: correct!
e would be a palindrome: correct!
de would not be a palindrome: correct!
1