C Example: Levenshtein Distance: DP matrix and edit costs

🗓 Last updated: 2026-11-30👤 Written by: Harper✓ Reviewed by: Dana🏷 Category: Algorithms

Computes edit distance with a 2D DP: dp[i][j] takes the minimum of delete, insert and substitute, with zero substitution cost for equal characters. Prints the full 8×8 matrix and distance 3 for kitten→sitting, then tests flaw→lawn and identical strings.

🔥 Advanced Reference entries: kw-intkw-charfn-strlen
🖥 Run OnlineGCC 10.2 · C11 standard · click ▶ to run · Copy code
🖨 Reference OutputMatches the online run in a standard C11 environment
C · GCC 10.2 · -std=c11
        _  s  i  t  t  i  n  g
  _ |   0  1  2  3  4  5  6  7
  k |   1  1  2  3  4  5  6  7
  i |   2  2  1  2  3  4  5  6
  t |   3  3  2  1  2  3  4  5
  t |   4  4  3  2  1  2  3  4
  e |   5  5  4  3  2  2  3  4
  n |   6  6  5  4  3  3  2  3
kitten   -> sitting   distance=3
        _  l  a  w  n
  _ |   0  1  2  3  4
  f |   1  1  2  3  4
  l |   2  1  2  3  4
  a |   3  2  1  2  3
  w |   4  3  2  1  2
flaw     -> lawn      distance=2
        _  C
  _ |   0  1
  C |   1  0
C        -> C         distance=0
🧩 Keep Learning

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏