카타 8급 Is it a palindrome?

1 C[ | ]

int isPalindrom (const char *s) {
  int len = strlen(s);
  int half = len/2;
  for(int i=0; i<half; i++) {
    if(tolower(s[i]) != tolower(s[len-1-i])) return 0;
  }
  return 1;
}

2 C++[ | ]

#include <string>
bool isPalindrom (const std::string& str)
{
  std::string lstr = str;
  for (char& c : lstr) c = toupper(c);
  return lstr == std::string(lstr.rbegin(), lstr.rend());
}
#include <string>
#include <locale>
#include <algorithm>
bool isPalindrom(const std::string& str) {
  const std::locale& loc = std::locale();
  const auto cmp = [&loc](char c1, char c2) { return tolower(c1, loc) == tolower(c2, loc); };
  return equal(str.begin(), str.begin() + str.length() / 2, str.rbegin(), cmp);
}
#include <string>
bool isPalindrom (const std::string& str)
{
  int len = str.length();
  int half = len/2;
  for(int i=0; i<half; i++) {
    if(tolower(str[i]) != tolower(str[len-1-i])) return false;
  }
  return true;
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}