Find Longest Common Substring

Write a Logic/Pseudo Code to find longest common substring from two given strings. i.e. Out of two strings, “I like New York City the most.” And “Who else like New York City as I do?”, “like New York City” is the longest common substring.

Showing Answers 1 - 1 of 1 Answers

perumal

  • May 24th, 2017
 

Code is attached

Code
  1. #include<stdio.h>

  2. #include<string.h>

  3. main()

  4. {

  5. char str1[100] = "I like New York City the most";

  6. char str2[200] = "Who else like New York City as I do";

  7. int matchstart=0;

  8. int maxmatch=0;

  9. int prevmatch=0;

  10. int prevstart=0;

  11. int i,j=0,k;

  12. for (k=0;k<strlen(str1);k++)

  13. {

  14. j=k;

  15. for (i=0;i<strlen(str2);i++)

  16. {

  17.         if (str1[j] == str2[i])

  18.         {

  19. //              printf("  %d",maxmatch);

  20.           maxmatch++;

  21.           if (0 == matchstart)

  22.           {

  23.              matchstart=i;

  24.           }

  25.           j++;

  26.  

  27.           if (j== strlen(str1))

  28.           {

  29.            break;

  30.           }

  31.         }

  32.         else

  33.         {

  34.           if (prevmatch < maxmatch)

  35.          {

  36.           prevmatch = maxmatch;

  37.           prevstart = matchstart;

  38.          }

  39.           maxmatch =0;

  40.           matchstart =0;

  41.           j=k;

  42.  

  43.         }

  44. }

  45. }

  46. //printf("

  47.  

  48.  pre%d max%d",prevmatch,maxmatch);

  49. if (prevmatch > maxmatch)

  50. {

  51.  for (i=prevstart;i<prevmatch+prevstart;i++)

  52.  {

  53.    printf("%c",str2[i]);

  54.  }

  55. }

  56. else if(maxmatch)

  57. {

  58.  for (i=matchstart;i<maxmatch;i++)

  59.  {

  60.    printf("%c",str2[i]);

  61.  }

  62. }

  63.  

  64. }

  65. ~

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Answer Question

Click here to Login / Register your free account


 
Send   Reset

 

Related Answered Questions

 

Related Open Questions