Advertisement
J3st3rs_j0k3

advrsrsvs

Jan 28th, 2021
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. #include <stdbool.h>
  6.  
  7.  
  8. int del_eq_sen(char** main, char** main_update, int count);
  9. int del_even(char**  first_text, char** new_text, int count);
  10. char ** plus(const void *a, const void* b);
  11. char **sentence(char *sent, int *count);
  12. char** strt_dig(char** txt_1, int * counter, int count);
  13. int comparator(const void* word1, const void* word2);
  14. void less_then_3(char **txt, int *count);
  15.  
  16. int main(){
  17.  
  18.     char* input_words;
  19.     char** initial_txt;
  20.     char** main_upd;
  21.     char** txt_rmvd;
  22.  
  23.     initial_txt = malloc(100*sizeof(char*));
  24.     char symb;
  25.     int n;
  26.  
  27.     //выделение памяти под строку с текстом
  28.     input_words = malloc(100*sizeof(char));
  29.     int size = 100;
  30.  
  31.     int i = 0, j, counter = -1, count = 0;
  32.  
  33.     //считывание символов с клавиатуры
  34.     while((symb = getchar()) != '\n') {
  35.         input_words[i] = symb;
  36.         i++;
  37.         if (i ==  size - 1){
  38.             size = size*2;
  39.             input_words = realloc(input_words, size);
  40.         }
  41.     }
  42.  
  43.     //выделение памяти нп массив указателей (предложения)
  44.     initial_txt = malloc(100*sizeof(char*));
  45.     int size_arr = 100;
  46.  
  47.     //разделение исходной строки на предложения
  48.     for(i = 0;i < strlen(input_words); i++){
  49.         counter++;
  50.         if(input_words[i] == '.'){
  51.             initial_txt[count] = malloc(counter*sizeof(char)+10);
  52.             for(j = 0, n = i - counter; n <= i ; n++, j++){
  53.                 initial_txt[count][j] = input_words[n];
  54.             }
  55.             initial_txt = realloc(initial_txt, 100*i);
  56.             initial_txt[count][j] = '\0';
  57.             count++;
  58.             if (count == size_arr - 1){
  59.                 size_arr = size_arr*2;
  60.                 initial_txt = realloc(initial_txt, size_arr);
  61.             }
  62.             counter = -1;
  63.             i++;
  64.         }
  65.     }
  66.  
  67.     main_upd = malloc(count * sizeof(char*));
  68.     count = del_eq_sen(initial_txt, main_upd, count);
  69.     initial_txt = main_upd;
  70.     printf("Текст без одинаковых предложений: ");
  71.     for(i = 0; i < count; i ++){
  72.         printf("%s\n", initial_txt[i]);
  73.     }
  74.     //выделение памяти под полученный массив после работы функции del_ev
  75.  
  76.     txt_rmvd = malloc(count*sizeof(char*));
  77.  
  78.  
  79.     int counterslova = 0;
  80.     int option;
  81.     printf("1 - Удалить все четные по счету предложения в которых четное количество слов.\n"
  82.            "2 - Отсортировать все слова в предложениях по возрастанию количества букв в верхнем регистре в слове.\n"
  83.            "3 - Заменить все слова в тексте длина которых не более 3 символов на подстроку “Less Then 3”.\n"
  84.            "4 - Найти в каждом предложении строку максимальной длины, которая начинается и заканчивается цифрой. Вывести найденные подстроки по убыванию длины подстроки.\n"
  85.            "5 - Выход из программы.\n");
  86.     // //считывание переменной, отвечающей за выбор функции, которую нужно исполнить
  87.      scanf("%d", &option);
  88.      switch(option){
  89.          case 1:
  90.              count = del_even(initial_txt, txt_rmvd, count);
  91.              break;
  92. ////         case 2:
  93. ////             qsort(initial_txt, count, sizeof(char*), comparator);
  94. ////                for(int i = 0; i < count; i++){
  95. ////                    printf("%s", main_update[i]);
  96. ////                }
  97. //
  98. //             break;
  99.          case 3:
  100.              less_then_3(initial_txt, &count);
  101.  
  102.              break;
  103.          case 4:
  104.              txt_rmvd = strt_dig(initial_txt, &counterslova, count);
  105.              count = counterslova;
  106.              break;
  107. //             abcb2 1a2. abcde 1abc2. 1def.
  108.          case 5:
  109.              printf("Программа завершена досрочно");
  110.              return 0;
  111.  
  112.          default:
  113.              printf("Данные некорректны");
  114.  
  115.      }
  116.     for(int g = 0; g < count; g++){
  117.         printf("%s ", txt_rmvd[g]);
  118.     }
  119.  
  120.     return 0;
  121. }
  122.  
  123. //удаление одинаковых предложений
  124. int del_eq_sen(char ** main, char**main_update, int count) {
  125.     int k, z, w = 0;
  126.     char*comp_1;
  127.     char*comp_2;
  128.     for (int i = 0; i < count; i++){
  129.         k = 0;
  130.         comp_1 = main[i];
  131.         for (z = 0; z < count-1; z++) {
  132.             if (z == i)
  133.                 z++;
  134.             if (strlen(main[i]) == strlen(main[z])) {
  135.                 comp_2 = main[z];
  136.                 for (int j  = 0; j < strlen(main[i]); j++){
  137.                     comp_1[j] = (char)(tolower(comp_1[j]));
  138.                     comp_2[j] = (char)(tolower(comp_2[j]));
  139.                 }
  140.                 if (strcmp(comp_1, comp_2) == 0) {
  141.                     k++;
  142.                 }
  143.             }
  144.         }
  145.         if (i != count) {
  146.             if (k == 0)
  147.             {
  148.                 main_update[w] = main[i];
  149.                 w++;
  150.             }
  151.         }
  152.  
  153.     }
  154.     return w;
  155. }
  156.  
  157.  
  158. //функция по удалению четных предложений с четным количеством слов
  159. int del_even(char**  first_text, char** new_text,int count){
  160.     int j; int i; int counter;
  161. //    int amount = count;
  162.     int g = 0;
  163.     for(j = 0; j < count; j++){
  164.         i = 0; counter = 0;
  165.         if(j%2 != 0){
  166.             while(first_text[j][i] != '.'){
  167.                 if(first_text[j][i] == ' ' || first_text[j][i] == ','){
  168.                     if(first_text[j][i] == ','){
  169.                         counter++;
  170.                         i++;
  171.                     }
  172.                     else{
  173.                         counter++;
  174.                     }
  175.                 }
  176.                 i++;
  177.             }
  178.             counter++;
  179.             if(counter%2 != 0){
  180. //                free(first_text[j]);
  181. //                amount--;
  182.                 new_text[g] = first_text[j];
  183.                 g++;
  184.             }
  185.         }
  186.         else{
  187.             new_text[g] = first_text[j];
  188.             g++;
  189.         }
  190.     }
  191.     return g;
  192. }
  193.  
  194.  
  195. //
  196. void less_then_3(char **txt, int *count){
  197. //    char** updatedtxt=malloc(*count_sent*sizeof(char));
  198. //    char** new_text = txt;
  199.     for (int i = 0; i < *count; i++) {
  200.         char* string = txt[i];
  201.         char* new_string = (char*)malloc(1 * sizeof(char));
  202.         new_string[0] = '\0';
  203.         int len_string = 0;
  204.         int len_word = 0;
  205.         int len_new_string = 0;
  206.         while (string[len_string] != '\0') {
  207.             if ((string[len_string] == ',') || (string[len_string] == '.') || (string[len_string] == ' ')) {
  208.                 if (len_word <= 3) {
  209.                     new_string = (char*)realloc(new_string, strlen(new_string) + 13 * sizeof(char));
  210.                     strcat(new_string, "Less Then 3");
  211.                     new_string[strlen(new_string) + 1] = '\0';
  212.                     new_string[strlen(new_string)] = string[len_string];
  213.                     len_new_string += 12;
  214.                 }
  215.                 else {
  216.                     new_string = (char*)realloc(new_string, strlen(new_string) + len_word + 2 * sizeof(char));
  217.                     for (int j = 0; j <= len_word; j++) {
  218.                         new_string[len_new_string] = string[len_string - len_word + j];
  219.                         len_new_string++;
  220.                     }
  221.                     new_string[len_new_string] = '\0';
  222.                 }
  223.                 len_word = -1;
  224.             }
  225.             len_word++;
  226.             len_string++;
  227.         }
  228.         txt[i] = new_string;
  229.  
  230.     }
  231.     for(int s = 0; s < *count; s++)
  232.         printf("%s ",txt[s]);
  233. }
  234. //gh gfyet. grwrwrh. das ada. daddad adad. 12rt erstt.
  235.  
  236. // int comparator(const void* word1, const void* word2){
  237.  
  238. //     char *x = * word1;
  239. //     char *y = * word2;
  240. //     for
  241.  
  242.  
  243.  
  244.  
  245. //         }
  246. //     return
  247. // }
  248.  
  249. // char** mass_words(char ** t_text, int count, func){
  250. //     char ** t_words = malloc(sizeof(char*));
  251. //     int i;
  252. //     int kolvo;
  253. //     for(i = 0; i < count; i++){
  254. //         t_words = func(t_text[i], &kolvo);
  255. //     }
  256. // }
  257.  
  258. //функция по нахождению наибольшей строки в предложении
  259. char** strt_dig(char** txt_1, int * counter, int count){
  260.     int k, j;
  261.     int i;
  262.     int numbs = 0;
  263.     char** dop_arr = NULL;
  264.     char** slova = malloc(sizeof(char*));
  265.     for(i = 0; i < count; i++){
  266.         char* oneword = NULL;
  267.         int amount = 0;
  268.         dop_arr = sentence(txt_1[i], &amount);
  269. //        printf("%s ", dop_arr[0]);
  270.         for (k = 0; k < amount; k++){
  271.             if(isdigit(dop_arr[k][0]) && isdigit(dop_arr[k][strlen(dop_arr[k])-1])){
  272.                 if(oneword == NULL){
  273.                     oneword = dop_arr[k];
  274. //                    printf("%s ", dop_arr[k]);
  275.                 }
  276.                 else{
  277.                     if(strlen(oneword) <= strlen(dop_arr[k])){
  278.                         oneword = dop_arr[k];
  279.  
  280.                     }
  281.                 }
  282.             }
  283.         }
  284.         slova[numbs++] = oneword;
  285.         slova = realloc(slova, (numbs+1)*sizeof(char*));
  286.     }
  287.     for(int kus = 0; kus < numbs - 1; kus++){
  288.         for(int gus = kus + 1; gus < numbs; gus++){
  289.             if(strlen(slova[kus]) < strlen(slova[gus])){
  290.                 char* buffer = slova[kus];
  291.                 slova[kus] = slova[gus];
  292.                 slova[gus] = buffer;
  293.             }
  294.         }
  295.     }
  296.     *counter = numbs;
  297.     return slova;
  298. }
  299. //qwe. qwe qwe. err rer. wqe qwe. err rer. qwe wqe. qweq qwe qw. qwe qwe qwe. qwe qe. wqe. qwe wq. hui sosem ebanniy vrot.
  300. char **sentence(char *sent, int *count){
  301.     char *istr = sent;
  302.     int id = 0;
  303.     char **senten = malloc (sizeof(char *));
  304.     istr = strtok(istr," ,.");
  305.     while (istr != NULL)
  306.     {
  307.         senten[id++] = istr;
  308.         senten = realloc(senten, (id + 1) * sizeof(char));
  309.         istr = strtok (NULL," ,.");
  310.     }
  311.     *count = id;
  312.     return senten;
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement