Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int find_word(char* p1, int len, char* s)
  4. {
  5. while(*s != '\0')
  6. {
  7. int check = 1;
  8. for(int i = 0; i < len; i++)
  9. {
  10. if(*(s+i) == '\0') return 0;
  11. if(*(s+i) != *(p1+i))
  12. {
  13. check = 0;
  14. break;
  15. }
  16. }
  17.  
  18. if(check == 1) return 1;
  19. s++;
  20. }
  21.  
  22. return 0;
  23. }
  24.  
  25. void longest_common_substring(char* s1, char* s2)
  26. {
  27. char* p1 = s1;
  28. char* p2 = s2;
  29. char* p3;
  30. char* start;
  31. char* end;
  32. int len1 = 0, len2 = 0;
  33.  
  34. while(*p1 != '\0')
  35. {
  36. if(*p1 >= 'A' && *p1 <= 'Z') *p1 -= 'a'-'A';
  37. p1++;
  38. len1++;
  39. }
  40. while(*p2 != '\0')
  41. {
  42. if(*p2 >= 'A' && *p2 <= 'Z') *p2 -= 'a'-'A';
  43. p2++;
  44. len2++;
  45. }
  46.  
  47. if(len1 > len2)
  48. {
  49. p1 = start = end = s2;
  50. p3 = s1;
  51. }
  52. else
  53. {
  54. p1 = start = end = s1;
  55. p3 = s2;
  56. }
  57.  
  58. while(*p1 != '\0')
  59. {
  60. p2 = p1;
  61. int counter = 1;
  62. while(*p2 != '\0')
  63. {
  64. if(find_word(p1, counter, p3) && p2-p1 > end-start)
  65. {
  66. start = p1;
  67. end = p2;
  68. }
  69. p2++;
  70. counter++;
  71. }
  72. p1++;
  73. }
  74.  
  75. while(start <= end)
  76. {
  77. printf("%c", *start);
  78. start++;
  79. }
  80. printf("\n");
  81. }
  82.  
  83. int main()
  84. {
  85. char* s1 = "abababcde";
  86. char* s2 = "cccdeccccccccc";
  87.  
  88. longest_common_substring(s1,s2);
  89.  
  90. return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement