Implementation of string fuzzy query method in C language

  • 2020-04-02 03:09:36
  • OfStack

String fuzzy query, mainly input incomplete information for the search, that is, every time the search is to query whether the contents of the contents of the input, if there is, it is found. The following detailed introduction of the fuzzy query implementation method, the code is as follows:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main(int argc, const char * argv[])
{
  char str[] = "hello welcome to china0"; //The source string
  printf("input a string:n");       
  char str2[20];              //The string to look for
  fgets(str2, 19, stdin);
  char *res;
  res = memchr(str, str2[0], strlen(str));  // According to the The string to look for First character, cut The source string
  if (res == NULL)
  {
    printf("find nothing...n");
    return 0;
  }
  
  int n;
  while (1)
  {
    n = memcmp(res, str2, strlen(str2) - 1); //To compare
    if (n != 0)
    {
      if (strlen(res) <= strlen(str2))  //The cut string is smaller than the length of the string to look for
      {
        printf("find nothing...n");
        return 0;
      }
      else
      {  
       //Continue cutting based on the first character to look for
        res = memchr(res + 1, str2[0], strlen(res));  
        if (res == NULL)
        {
          printf("find nothing...n");
          return 0;
        }
         
      }
    }
    else
    { //If n is equal to 0, find
      printf("%s is found..n", str2);
      return 0;
    }
  }
}

Through the above specific implementation code, I hope you can understand, to help you.


Related articles: