Skip to content

wooorm/levenshtein.c

Repository files navigation

levenshtein(3)

Build Coverage

Vladimir Levenshtein’s edit distance algorithm1 as a C library. There’s also a CLI: levenshtein(1), and a JavaScript version.

Installation

CLib:

$ clib install wooorm/levenshtein.c

Or clone the repo.

Usage

size_t levenshtein(const char *a, const char *b);

#include <stdio.h>
#include "levenshtein.h"

int
main(int argc, char **argv) {
  char *a = argv[1];
  char *b = argv[2];

  if (argc < 3) {
    fprintf(stderr, "\033[31mLevenshtein expects two arguments\033[0m\n");

    return 1;
  }

  printf("%zu\n", levenshtein(a, b));
}

size_t levenshtein_n(const char *a, const size_t length, const char *b, const size_t bLength);

#include <stdio.h>
#include "levenshtein.h"

int
main() {
  const char *a = "foobar";
  const char *b = "hello";

  printf("%zu\n", levenshtein_n(a, 6, b, 5));
}

License

MIT © Titus Wormer