카타 7급 Partial Word Searching

C[ | ]

#include <stdlib.h>
char** word_search(const char* query, const char** strings, size_t count, size_t* pResultCount) {
  char** matching = malloc(count * sizeof(char*));
  *pResultCount = 0;
  for (int i=0; i<count; ++i) {
    if (strcasestr(strings[i], query)) {
      matching[(*pResultCount)++] = strdup(strings[i]);
    }
  }
  if (*pResultCount == 0) matching[(*pResultCount)++] = strdup("None");
  return realloc(matching, *pResultCount * sizeof(char*));
}
#include <stdlib.h>
char **word_search(const char *query, const char **strings, size_t count, size_t *pResultCount) {  
  const char** res = calloc(count, sizeof(char*));
  *pResultCount = 0;
  for (int i = 0; i < count; i++) {
    if (strcasestr(strings[i], query) != NULL) res[(*pResultCount)++] = strings[i];
  }
  if (*pResultCount == 0) res[(*pResultCount)++] = "None";
  return res;
}
#include <stdlib.h>
int contains(char* needle, char* haystack, int len1, int len2) {
  if( len1>len2 ) return 0;
  for(int i=0; i<len2-len1+1; i++) {
    int same = 1;
    for(int j=i; j<i+len1; j++) {
      if( tolower(haystack[i+j]) != tolower(needle[j]) ) same = 0;
    }
    if( same ) return 1;
  }
  return 0;
}
char **word_search(const char *query, const char **strings, size_t count, size_t *pResultCount) {
  char **res[count];
  int pos = 0;
  int len1 = strlen(query);
  for(int i=0; i<count; i++) {
    int len2 = strlen(strings[i]);
    if( len1>len2 || !contains(query,strings[i],len1,len2) ) continue;
    res[pos++] = strings[i];
  }
  if( pos<1 ) res[pos++] = "None";
  *pResultCount = pos;
  return res;
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}