fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4.  
  5.  
  6. int longest_common_subsequence( char *str1, char *str2, int k, int l )
  7. {
  8. int longest[k + 1][l + 1];
  9. int i, j;
  10.  
  11. for (i = 0; i <= k; i++)
  12. { for (j = 0; j <= l; j++)
  13. {
  14. if (i == 0 || j == 0)
  15. longest[i][j] = 0;
  16. else if (str1[i - 1] ==str2[j - 1])
  17. longest[i][j] = longest[i - 1][j - 1] + 1;
  18. else
  19. longest[i][j] = max(longest[i - 1][j], longest[i][j - 1]);
  20. }
  21. }
  22. return longest[k][l];
  23. }
  24.  
  25. int max(int a, int b)
  26. {
  27. if(a > b)
  28. return a;
  29. else
  30. return b;
  31. }
  32.  
  33. int main()
  34. {
  35. char str1[100];char str2[100];
  36. printf("enter two strings to be compared");
  37. scanf("%s %s",&str1,&str2);
  38.  
  39.  
  40. int i = strlen(str1);int j = strlen(str2);
  41. int length= longest_common_subsequence( str1, str2, i, j );
  42. printf("the lenght of the longest common subsequence is: %d",length);
  43.  
  44. return 0;
  45. }
  46.  
  47.  
Success #stdin #stdout 0s 4380KB
stdin
AGGTAB GXTXAYB
stdout
enter two strings to be comparedthe lenght of the longest common subsequence is: 4