[ create a new paste ] login | about

Link: http://codepad.org/vBSFYyVa    [ raw code | output | fork ]

C++, pasted on Dec 1:
#include <iostream>
#include <string>
#include <new>
#include <cctype>

bool is_palindrome(std::string const s) {
  std::string::const_iterator st_stt = s.begin();
  std::string::const_iterator st_end = s.end();
  for (;(!::isalpha(*st_stt)) && st_stt < st_end; ++st_stt)
    ;
  do {
    --st_end; } while ((!::isalpha(*st_end)) && st_stt < st_end); 

  if (st_end <= st_stt)
    return true;
  if (::toupper(*st_stt) != ::toupper(*st_end))
    return false;

  return is_palindrome(std::string(++st_stt, st_end)); /* no need of decriment for st_end */
}

void check_palindrome(std::string cs) {
  std::cout << '\"' << cs << "\" " <<  (is_palindrome(cs) ? "is " : "is NOT ") << "palindrome." << std::endl;
}

int main() {
  check_palindrome("boo");
  check_palindrome("A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal-Panama!");
  check_palindrome("A tin mug for a jar of gum, Nita.");
  check_palindrome("A Toyota! Race fast, safe car! A Toyota!");
  check_palindrome("boo      oob");
  return 0;
}
/* end */


Output:
1
2
3
4
5
"boo" is NOT palindrome.
"A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal-Panama!" is palindrome.
"A tin mug for a jar of gum, Nita." is palindrome.
"A Toyota! Race fast, safe car! A Toyota!" is palindrome.
"boo      oob" is palindrome.


Create a new paste based on this one


Comments: