Advertisement
J3st3rs_j0k3

pr_cw_not final 9

Dec 27th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <ctype.h>
  6.  
  7. int del_eq_sen(char** main, char** main_update, int count);
  8. int del_even(char**  first_text, int count);
  9. char ** plus(const void *a, const void* b);
  10.  
  11. int main(){
  12.    
  13.     char* input_words;
  14.     char** initial_txt;
  15.     char** main_upd;
  16.     char** txt_after_remove;
  17.    
  18.     initial_txt = malloc(100*sizeof(char*));
  19.     char symb;
  20.     int n;
  21.    
  22.     //выделение памяти под строку с текстом
  23.     input_words = malloc(10*sizeof(char));
  24.     int size = 100;
  25.    
  26.     int i = 0, j, counter = -1, count = 0;
  27.    
  28.     //считывание символов с клавиатуры
  29.     while((symb = getchar()) != '\n') {
  30.         input_words[i] = symb;
  31.         i++;
  32.         if (i ==  size - 1){
  33.             size = size*2;
  34.             input_words = realloc(input_words, size);
  35.         }
  36.     }
  37.    
  38.     //выделение памяти нп массив указателей (предложения)
  39.     initial_txt = malloc(100*sizeof(char*));
  40.     int size_arr = 100;
  41.    
  42.     //разделение исходной строки на предложения
  43.     for(i = 0;i < strlen(input_words); i++){
  44.         counter++;
  45.         if(input_words[i] == '.'){
  46.             initial_txt[count] = malloc(counter*sizeof(char)+10);
  47.             for(j = 0, n = i - counter; n <= i ; n++, j++){
  48.                 initial_txt[count][j] = input_words[n];
  49.             }
  50.             initial_txt = realloc(initial_txt, 100*i);
  51.             initial_txt[count][j] = '\0';
  52.             count++;
  53.             if (count == size_arr - 1){
  54.                 size_arr = size_arr*2;
  55.                 initial_txt = realloc(initial_txt, size_arr);
  56.             }
  57.             counter = -1;
  58.             i++;
  59.         }
  60.     }
  61.    
  62.     main_upd = malloc(count * sizeof(char*));
  63.     int c1 = del_eq_sen(initial_txt, main_upd, count);
  64.     initial_txt = main_upd;
  65.     for(i = 0; i < count; i ++){
  66.         printf("%s ", initial_txt[i]);
  67.     }
  68.     //выделение памяти под полученный массив после работы функции del_eq_sen
  69.    
  70.     txt_after_remove = malloc(count*sizeof(char*));
  71.    
  72.     //for(int g = 0; g < c1; g++){
  73.     //    printf("%s ", main_upd[g]);
  74.     // }
  75.     int giv = del_even(initial_txt, c1);
  76.    
  77.     int option;
  78.     printf("1 - Удалить все четные по счету предложения в которых четное количество слов.\n"
  79.           "2 - Отсортировать все слова в предложениях по возрастанию количества букв в верхнем регистре в слове.\n"
  80.           "3 - Заменить все слова в тексте длина которых не более 3 символов на подстроку “Less Then 3”.\n"
  81.           "4 - Найти в каждом предложении строку максимальной длины, которая начинается и заканчивается цифрой. Вывести найденные подстроки по убыванию длины подстроки.\n"
  82.           "5 - Выход из программы.\n");
  83.     // //считывание переменной, отвечающей за выбор функции, которую нужно исполнить
  84.     // scanf("%d", &option);
  85.     // switch(option){
  86.     //     case 1:
  87.            
  88.     //         break;
  89.     //     case 2: qsort(main_update, c1, sizeof(char*), plus);
  90.                 //   for(int i = 0; i < c1; i++){
  91.                 //       printf("%s", main_update[i]);
  92.                 //   }
  93.            
  94.     //         break;
  95.     //     case 3:
  96.            
  97.     //         break;
  98.     //     case 4:
  99.        
  100.     //         break;
  101.     //     case 5:
  102.     //         printf("Программа завершена досрочно");
  103.     //         return 0;
  104.     //     default:
  105.     //         printf("Данные некорректны");
  106.        
  107.     // }
  108.    
  109.     return 0;
  110. }
  111.  
  112. //удаление одинаковых предложений
  113. int del_eq_sen(char ** main, char**main_update, int count) {
  114.     int k, z, w = 0;
  115.     char*comp_1;
  116.     char*comp_2;
  117.     for (int i = 0; i < count; i++){
  118.         k = 0;
  119.         comp_1 = main[i];
  120.         for (z = 0; z < count-1; z++) {
  121.             if (z == i)
  122.                 z++;
  123.             if (strlen(main[i]) == strlen(main[z])) {
  124.                 comp_2 = main[z];
  125.                 for (int j  = 0; j < strlen(main[i]); j++){
  126.                     comp_1[j] = (char)(tolower(comp_1[j]));
  127.                     comp_2[j] = (char)(tolower(comp_2[j]));
  128.                 }
  129.                 if (strcmp(comp_1, comp_2) == 0) {
  130.                     k++;
  131.                 }
  132.             }
  133.         }
  134.         if (i != count) {
  135.             if (k == 0)
  136.             {
  137.                 main_update[w] = main[i];
  138.                 w++;
  139.             }
  140.         }
  141.  
  142.     }
  143.     return w;
  144. }
  145.  
  146.  
  147. //функция по удалению четных предложений с четным количеством слов
  148. int del_even(char**  first_text, int count){
  149.     int j; int i = 1; int counter = 0;
  150.     int amount = count;
  151.     for(j = 1; j < count; j += 2){
  152.         while(first_text[j][i] != '.'){
  153.             if(first_text[j][i] == ' ' || first_text[j][i] == ','){
  154.                 if(first_text[j][i] == ','){
  155.                     counter++;
  156.                     i++;
  157.                 }
  158.                 else{
  159.                     counter++;
  160.                 }
  161.             }
  162.             i++;
  163.         }
  164.         counter++;
  165.         if(counter%2 == 0){
  166.             free(first_text[j]);
  167.             amount--;
  168.         }
  169.     }
  170.     return amount;
  171. }
  172.  
  173.  
  174. // char ** plus(const void *a, comst void *b, int count){
  175. //     char **x = (char **) a;
  176. //     char **y = (char **) b;
  177.         // counter = 0;
  178. //     //char **y = (char **) b;
  179. //     for (int i = 0; i < count; i++){
  180. //         for(int k = 0; k < strlen(array[i]); k++){
  181. //             while(array[j][k] == ' ' || array[j][k] == ", " || array[j][k] == '.'){
  182. //                 if(isalpha(array[j][k]) == TRUE && isupper(array[j][k]) == TRUE){
  183. //                     counter++;
  184. //                 }
  185. //             }
  186. //         }
  187.        
  188. //     }
  189.    
  190.  
  191. //     return
  192. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement