Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int getline(char s[], int lim)
  5. {
  6.     int i;
  7.     char c;
  8.     for (i = 0; i<lim && (c = getchar()) != EOF && c != '\n'; ++i)
  9.         s[i] = c;
  10.     s[i] = '\0';
  11.     while (c != EOF && c != '\n')
  12.         c = getchar();
  13.     return i;
  14. }
  15.  
  16. int containsChar(char *strng, char c){
  17.     int i;
  18.     int strng_length = strlen(strng);
  19.     for (i = 0; i < strng_length; i++)
  20.     {
  21.         if (strng[i] == c){
  22.             return 1;
  23.         }
  24.     }
  25.     return 0;
  26. }
  27.  
  28. #define MAX 100
  29. char* removee(char *s, int i, int n){
  30.     int s_length = strlen(s);
  31.     int j;
  32.     char result[MAX + 1];
  33.  
  34.     strcpy_s(result, MAX, s);
  35.     if (s_length >= i + n){
  36.         for (j = i; j < s_length - n; j++)
  37.         {
  38.             result[j] = result[j + n];
  39.         }
  40.         result[j] = '\0';
  41.         return result;
  42.     }
  43.     else{
  44.         return "";
  45.     }
  46. }
  47.  
  48. char* getLongestChildrenString(char *s1, char *s2, int current_longest){
  49.     char result[MAX + 1];
  50.     char temp_result[MAX + 1];
  51.     char test[MAX + 1];
  52.     char *in, s1_copy[MAX + 1], s2_copy[MAX + 1];
  53.     int i;
  54.  
  55.     if (strlen(s1) == 0 || strlen(s2) == 0){
  56.         return "";
  57.     }
  58.  
  59.     if (current_longest >= strlen(s1) || current_longest >= strlen(s2)){
  60.         return "";
  61.     }
  62.  
  63.     for (i = 0; i < strlen(s1); i++)
  64.     {
  65.         if (containsChar(s2, s1[i]) == 0){
  66.             strcpy_s(s1, MAX, removee(s1, i, 1));
  67.             i--;
  68.         }
  69.     }
  70.     for (i = 0; i < strlen(s2); i++)
  71.     {
  72.         if (containsChar(s1, s2[i]) == 0)
  73.         {
  74.             strcpy_s(s2, MAX, removee(s2, i, 1));
  75.             i--;
  76.         }
  77.     }
  78.     strcpy_s(result, MAX, "");
  79.  
  80.     in = strstr(s1, s2);
  81.     if (in){
  82.         return s2;
  83.     }
  84.     in = strstr(s2, s1);
  85.     if (in){
  86.         return s1;
  87.     }
  88.  
  89.     for (i = 0; i < strlen(s1); i++)
  90.     {
  91.         strcpy_s(test, MAX, removee(s1, i, 1));
  92.         strcpy_s(s2_copy, MAX, s2);
  93.         strcpy_s(temp_result, MAX, getLongestChildrenString(test, s2_copy, current_longest >= strlen(result) ? current_longest : strlen(result)));
  94.         if (strlen(temp_result) > strlen(result))
  95.         {
  96.             strcpy_s(result, MAX, temp_result);
  97.         }
  98.     }
  99.  
  100.     for (i = 0; i < strlen(s2); i++)
  101.     {
  102.         strcpy_s(test, MAX, removee(s2, i, 1));
  103.         strcpy_s(s1_copy, MAX, s1);
  104.         strcpy_s(temp_result, MAX, getLongestChildrenString(s1_copy, test, current_longest >= strlen(result) ? current_longest : strlen(result)));
  105.         if (strlen(temp_result) > strlen(result)){
  106.             strcpy_s(result, MAX, temp_result);
  107.         }
  108.  
  109.     }
  110.     return result;
  111.  
  112. }
  113.  
  114. int main(int argc, char *argv[]){
  115.     char s1[MAX + 1];
  116.     char s2[MAX + 1];
  117.     char longest_child[MAX + 1];
  118.  
  119.     printf("Adja meg az elso sztringet: ");
  120.     getline(s1, MAX);
  121.     printf("Adja meg az masodik sztringet: ");
  122.     getline(s2, MAX);
  123.  
  124.     strcpy_s(longest_child, MAX, getLongestChildrenString(s1, s2, 0));
  125.  
  126.     if (strcmp(longest_child, "") != 0){
  127.         printf("A leghosszabb gyereksztring: %s\n", longest_child);
  128.     }
  129.     else{
  130.         printf("Nincs a ket szonak kozos gyereksztringje!\n");
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement