--- title: "Levenstein Distance" type: "article" slug: "levenstein-distance" url: "http://localhost/article/levenstein-distance/" markdown_url: "http://localhost/article/levenstein-distance.md" published_at: "2025-11-26T19:43:56+00:00" modified_at: "2026-06-21T23:26:32+00:00" featured_image: url: "http://localhost/wp-content/uploads/2022/04/20230809-041332.jpg" excerpt: "The Levenstein Distance is a measure of how close two strings are to each other. The Levenstein Algoritm is used to calculate the Levenstein Distance. The Levenstein Algorithm takes two strings and determines what it would take to transform one string into the other, using deletions, additions, and changing characters. The more that must be…" category: - name: "QL Hacker's Journal" slug: "ql-hackers-journal" taxonomy: "category" url: "http://localhost/category/periodicals/ql-hackers-journal/" post_tag: - name: "C (programming language)" slug: "c-programming-language" taxonomy: "post_tag" url: "http://localhost/tag/c-programming-language/" - name: "Full Text" slug: "fulltext" taxonomy: "post_tag" url: "http://localhost/tag/fulltext/" - name: "QL" slug: "ql" taxonomy: "post_tag" url: "http://localhost/tag/ql/" - name: "Type-in program" slug: "type-in-program" taxonomy: "post_tag" url: "http://localhost/tag/type-in-program/" model: - name: "Sinclair QL" slug: "sinclair-ql" taxonomy: "model" url: "http://localhost/model/sinclair-ql/" indiv: - name: "Tim Swenson" slug: "tim-swenson" taxonomy: "indiv" url: "http://localhost/indiv/tim-swenson/" publication_r: id: 33686 title: "QL Hacker’s Journal" type: "periodical" url: "http://localhost/periodical/ql-hackers-journal/" authors_r: - name: "Tim Swenson" slug: "tim-swenson" taxonomy: "indiv" url: "http://localhost/indiv/tim-swenson/" issue: "6" issues_articles: - id: 61542 title: "QL Hacker’s Journal 6" type: "issue" url: "http://localhost/issue/ql-hackers-journal-6/" pubdate: "November 1991" archive_link: true --- # Levenstein Distance The Levenstein Distance is a measure of how close two strings are to each other. The Levenstein Algoritm is used to calculate the Levenstein Distance. The Levenstein Algorithm takes two strings and determines what it would take to transform one string into the other, using deletions, additions, and changing characters. The more that must be done, the less alike the two strings are. In QHJ #1 the Ratcliff/Obershelp algorithm for inexact pattern matching was discussed. This algorithm determines how close two strings are by recursively finding the largest equal substrings in the two strings. The larger substrings found, the closer the two strings are. This program comes from the May 1991 issue of the C Users Journal. The text accompanying the article explains the program better than I do. If you have questions, you should refer back to the original article. ``` /* ldistance() Determine to what extent two charcter strings are (un)equal using the 'Levenstein distance' algorithm. */ #include #include #include int addition = 1; int change = 3; int deletion = 5; #define COMP_LEN 20 #define ARR_SIZE COMP_LEN + 1 #define SMALLEST_OF(x,y,z) ( (xCOMP_LEN ? COMP_LEN : strlen(requested)); f_len = (strlen(found)>COMP_LEN ? COMP_LEN : strlen(found)); distance[0][0] = 0; for (j = 1; j <= ARR_SIZE; j++) distance[0][j] = distance[0][j-1] + addition; for (j = 1; j <= ARR_SIZE; j++) distance[j][0] = distance[j-1][0] + deletion; for (i = 1; i <= r_len; i++) for (j = 1; j <= f_len; j++) distance[i][j] = SMALLEST_OF( (distance[i-1][j-1] + ZERO_IF_EQUAL(i,j)), (distance[i][j-1] + addition), (distance[i-1][j] + deletion) ); return( distance[r_len][f_len] ); } int main() { int result; printf("Comparing '%s' and '%s' : \n","pennsylvania", "pencilvaneya"); result = ldistance("pennsylvania","pencilvaneya"); printf(" Result = %d\n",result); } ```