카타 7급 String ends with?

1 C[ | ]

#include <stdbool.h>
bool solution(const char *string, const char *ending)
{
  int len1 = strlen(string);
  int len2 = strlen(ending);
  int diff = len1 -len2;
  if( diff < 0 ) return false;
  for(int i=0; i<len2; i++) {
    if( ending[i] != string[diff+i] ) return false;
  }
  return true;
}

2 C++[ | ]

bool solution(std::string const &str, std::string const &ending) {
  return str.size() >= ending.size() && str.compare(str.size()-ending.size(), string::npos, ending) == 0;
}
bool solution(std::string const &str, std::string const &ending) {
  return ending == string(str.end()-ending.size(),str.end());
}
bool solution(std::string const &str, std::string const &ending) {
  int l1 = str.length();
  int l2 = ending.length();
  if( l1 < l2 ) return false;
  return ending.compare( str.substr(l1-l2, l2) ) == 0;
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}