fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int max(int x, int y) {
  5. return (x > y) ? x : y;
  6. }
  7.  
  8. float calculateSimilarityScore(char *Str1, char *Str2) {
  9. int m = strlen(Str1);
  10. int n = strlen(Str2);
  11.  
  12. int LCS[m + 1][n + 1];
  13. for (int i = 0; i <= m; i++) {
  14. for (int j = 0; j <= n; j++) {
  15. if (i == 0 || j == 0) {
  16. LCS[i][j] = 0;
  17. } else if (Str1[i - 1] == Str2[j - 1]) {
  18. LCS[i][j] = LCS[i - 1][j - 1] + 1;
  19. } else {
  20. LCS[i][j] = max(LCS[i - 1][j], LCS[i][j - 1]);
  21. }
  22. }
  23. }
  24.  
  25. float SimilarityScore = ((float)LCS[m][n] / max(m, n)) * 100.0;
  26.  
  27. return SimilarityScore;
  28. }
  29.  
  30. int main() {
  31. char Str1[1001], Str2[1001];
  32. printf("Enter the Doc1: ");
  33. fgets(Str1, 1001, stdin);
  34. Str1[strcspn(Str1, "\n")] = 0;
  35.  
  36. printf("Enter the Doc2: ");
  37. fgets(Str2, 1001, stdin);
  38. Str2[strcspn(Str2, "\n")] = 0;
  39.  
  40. float SimilarityScore = calculateSimilarityScore(Str1, Str2);
  41. printf("Similarity Score: %.2f%%\n", SimilarityScore);
  42.  
  43. return 0;
  44. }
Success #stdin #stdout 0s 5300KB
stdin
Alice was beginning to get very tired of sitting by her sister on the bank and of having nothing to do
Alice was getting tired of sitting by her sister on the riverbank with nothing to do
stdout
Enter the Doc1: Enter the Doc2: Similarity Score: 71.57%