Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <locale.h>
  4. #pragma warning(disable: 4996)
  5.  
  6. void Begin(char string[], int begin, int end, int *f);
  7. void End(char string[], int begin, int end, int *f);
  8. void Print(char string[], int k, int l, int n, int m);
  9. int Min(int begin1, int end1, int begin2, int end2);
  10. int Find(char string[], int i);
  11. char* ReadLine(void);
  12.  
  13.  
  14.  
  15. int main(void)
  16. {
  17.     char *str;
  18.     int position, beginWord = 0, word = 0;
  19.     int flag, f = 0;
  20.     setlocale(LC_ALL, "Russian");
  21.     do {
  22.         printf("Введите строку: ");
  23.         str = ReadLine();
  24.         printf("Результат: ");
  25.         for (position = 0; str[position] != 0; position++)
  26.         {
  27.             if (Find(str, position) != 0)
  28.                 word = word + 1;
  29.             else
  30.             {
  31.                 Begin(str, beginWord, word, &f);
  32.                 End(str, beginWord, word, &f);
  33.                 word = word + 1;
  34.                 beginWord = word;
  35.             }
  36.         }
  37.         printf("\n");
  38.         word = 0;
  39.         beginWord = 0;
  40.         f = 0;
  41.         flag = str[0] != '\n';
  42.         free(str);
  43.     } while (flag);
  44.     return 0;
  45. }
  46.  
  47. char* ReadLine(void)
  48. {
  49.     int i = 0, j = 1;
  50.     char *str, *fstr,  s;
  51.     str = (char*)malloc(j * sizeof(char));
  52.     do {
  53.         s = getchar();
  54.         fstr = str;
  55.         j++;
  56.         str = (char*)realloc(str, j * sizeof(char));
  57.         if (str == NULL)
  58.             free(fstr);
  59.         str[i] = s;
  60.         i++;
  61.     } while (s != '\n');
  62.     str[i] = '\0';
  63.     return str;
  64. }
  65.  
  66. void Begin(char string[], int begin, int end, int *f)
  67. {
  68.     int i, e = 0, b = 0, beg = begin, min, w = b, j = 0;
  69.     for (i = 0; i < begin; i++)
  70.     {
  71.         if (Find(string, i) != 0)
  72.             e = e + 1;
  73.         else
  74.         {
  75.             min = Min(w, e, beg, end);
  76.             for (i = min; i > 0; i--)
  77.             {
  78.                 if (string[beg] == string[e - i])
  79.                 {
  80.                     beg++;
  81.                     j++;
  82.                 }
  83.                 else
  84.                     beg = begin;
  85.             }
  86.             if ((beg != begin)&&(j > 1))
  87.             {
  88.                    
  89.                 if (*f != 0)
  90.                     printf(",");
  91.                 if (*f != 0)
  92.                     printf(" ");
  93.                 Print(string, b, begin + j, e, end);
  94.                 ++(*f);
  95.             }
  96.             e = e + 1;
  97.             b = e;
  98.             i = b - 1;
  99.             w = b;
  100.             beg = begin;
  101.             j = 0;
  102.         }
  103.     }
  104. }
  105.  
  106. void End(char string[], int begin, int end, int *f)
  107. {
  108.     int b = end + 1, e = end, beg = begin, i, w = end, min, j = 0;
  109.     for (i = b; string[i] != 0; i++)
  110.     {
  111.         if (Find(string, i) != 0)
  112.             e = e + 1;
  113.         else
  114.         {
  115.             min = Min(w, e, begin, end);
  116.             for (i = min; i > 0; i--)
  117.             {
  118.                 if (string[beg] == string[e + 1 - i])
  119.                 {
  120.                     beg++;
  121.                     j++;
  122.                 }
  123.                 else
  124.                     beg = begin;
  125.             }
  126.             if ((beg != begin) && (j > 1))
  127.             {
  128.                 if (*f != 0)
  129.                     printf(",");
  130.                 if (*f != 0)
  131.                     printf(" ");
  132.                 Print(string, b, begin + j, e + 1, end);
  133.                 ++(*f);
  134.             }
  135.             e = e + 1;
  136.             b = e + 1;
  137.             i = e;
  138.             w = b;
  139.             beg = begin;
  140.             j = 0;
  141.         }
  142.     }
  143. }
  144.  
  145. void Print(char word[], int k, int l, int n, int m)
  146. {
  147.     int i, j;
  148.     for (i = k; i < n; i++)
  149.         printf("%c", word[i]);
  150.     for (j =l; j < m; j++)
  151.         printf("%c", word[j]);
  152. }
  153.  
  154. int Min(int begin1, int end1, int begin2, int end2)
  155. {
  156.     int min;
  157.     if ((end1 - begin1) < (end2 - begin2))
  158.         min = end1 - begin1;
  159.     else
  160.         min = end2 - begin2;
  161.     return min;
  162. }
  163.  
  164. int Find(char string[], int i)
  165. {
  166.     return ((string[i] >= 'A' && string[i] <= 'Z') || (string[i] >= 'a' && string[i] <= 'z') || (string[i] >= '0' && string[i] <= '9'));
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement