Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int find_word(char* p1, int len, char* s)
- {
- while(*s != '\0')
- {
- int check = 1;
- for(int i = 0; i < len; i++)
- {
- if(*(s+i) == '\0') return 0;
- if(*(s+i) != *(p1+i))
- {
- check = 0;
- break;
- }
- }
- if(check == 1) return 1;
- s++;
- }
- return 0;
- }
- void longest_common_substring(char* s1, char* s2)
- {
- char* p1 = s1;
- char* p2 = s2;
- char* p3;
- char* start;
- char* end;
- int len1 = 0, len2 = 0;
- while(*p1 != '\0')
- {
- if(*p1 >= 'A' && *p1 <= 'Z') *p1 -= 'a'-'A';
- p1++;
- len1++;
- }
- while(*p2 != '\0')
- {
- if(*p2 >= 'A' && *p2 <= 'Z') *p2 -= 'a'-'A';
- p2++;
- len2++;
- }
- if(len1 > len2)
- {
- p1 = start = end = s2;
- p3 = s1;
- }
- else
- {
- p1 = start = end = s1;
- p3 = s2;
- }
- while(*p1 != '\0')
- {
- p2 = p1;
- int counter = 1;
- while(*p2 != '\0')
- {
- if(find_word(p1, counter, p3) && p2-p1 > end-start)
- {
- start = p1;
- end = p2;
- }
- p2++;
- counter++;
- }
- p1++;
- }
- while(start <= end)
- {
- printf("%c", *start);
- start++;
- }
- printf("\n");
- }
- int main()
- {
- char* s1 = "abababcde";
- char* s2 = "cccdeccccccccc";
- longest_common_substring(s1,s2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement