C++ Program to Implement Wagner and Fisher Algorithm for online String Matching

«
»
This is a C++ Program to implement online search. The Wagner–Fischer algorithm is a dynamic programming algorithm that measures the Levenshtein distance between two strings of characters.
For example, the Levenshtein distance between “kitten” and “sitting” is 3, since the following three edits change one into the other, and there is no way to do it with fewer than three edits.

Here is source code of the C++ Program to Implement Wagner and Fisher Algorithm for online String Matching. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <string.h>
  4. int d[100][100];
  5. #define MIN(x,y) ((x) < (y) ? (x) : (y))
  6. int main()
  7. {
  8.     int i,j,m,n,temp,tracker;
  9.     char s[] = "Sanfoundry";
  10.     char t[] = "Education";
  11.     m = strlen(s);
  12.     n = strlen(t);
  13.  
  14.     for(i=0;i<=m;i++)
  15.     d[0][i] = i;
  16.     for(j=0;j<=n;j++)
  17.     d[j][0] = j;
  18.  
  19.     for (j=1;j<=m;j++)
  20.     {
  21.         for(i=1;i<=n;i++)
  22.         {
  23.             if(s[i-1] == t[j-1])
  24.             {
  25.                 tracker = 0;
  26.             }
  27.             else
  28.             {
  29.                 tracker = 1;
  30.             }
  31.             temp = MIN((d[i-1][j]+1),(d[i][j-1]+1));
  32.             d[i][j] = MIN(temp,(d[i-1][j-1]+tracker));
  33.         }
  34.     }
  35.     printf("the Levinstein distance is %d\n",d[n][m]);
  36.     return 0;
  37. }

Output:

advertisement
$ g++ WagnerFischer.cpp
$ a.out
 
the Levinstein distance is 9
 
------------------
(program exited with code: 0)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement

Here’s the list of Best Reference Books in C++ Programming, Data Structures and Algorithms.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn