Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <stdbool.h>
- #include <locale.h>
- int del_eq_sen(char** main, char** main_update, int count);
- int del_even(char** first_text, char** new_text, int count);
- char **sentence(char *sent, int *count);
- char** strt_dig(char** txt_1, int * counter, int count);
- void less_then_3(char **txt, int *count);
- int sup_func(const void* a, const void* b);
- void sorting(char** answer, int car);
- void dig_str(char** text, int count);
- int main(){
- setlocale(LC_ALL, "Russian");
- char* input_words;
- char** initial_txt;
- char** main_upd;
- char** txt_rmvd;
- initial_txt = malloc(100*sizeof(char*));
- char symb;
- int n;
- int govno;
- //выделение памяти под строку с текстом
- input_words = malloc(100*sizeof(char));
- int size = 100;
- int i = 0, j, counter = -1, count = 0;
- //считывание символов с клавиатуры
- while((symb = getchar()) != '\n') {
- input_words[i] = symb;
- i++;
- if (i == size - 1){
- size = size*2;
- input_words = realloc(input_words, (size+2)*sizeof(char));
- }
- }
- //выделение памяти нп массив указателей (предложения)
- initial_txt = malloc(100*sizeof(char*));
- int size_arr = 100;
- //разделение исходной строки на предложения
- for(i = 0;i < strlen(input_words); i++){
- counter++;
- if(input_words[i] == '.'){
- initial_txt[count] = malloc(counter*sizeof(char)+10);
- for(j = 0, n = i - counter; n <= i ; n++, j++){
- initial_txt[count][j] = input_words[n];
- }
- initial_txt = realloc(initial_txt, 100*i*sizeof(char));
- initial_txt[count][j] = '\0';
- count++;
- if (count == size_arr - 1){
- size_arr = size_arr*2;
- initial_txt = realloc(initial_txt, size_arr*sizeof(char));
- }
- counter = -1;
- i++;
- }
- }
- main_upd = malloc(count * sizeof(char*));
- count = del_eq_sen(initial_txt, main_upd, count);
- initial_txt = main_upd;
- printf("Текст без одинаковых предложений: ");
- for(i = 0; i < count; i ++){
- printf("%s\n", initial_txt[i]);
- }
- //выделение памяти под полученный массив после работы функции del_ev
- txt_rmvd = malloc(count*sizeof(char*));
- int counterslova = 0;
- int option;
- printf("1 - Удалить все четные по счету предложения в которых четное количество слов.\n"
- "2 - Отсортировать все слова в предложениях по возрастанию количества букв в верхнем регистре в слове.\n"
- "3 - Заменить все слова в тексте длина которых не более 3 символов на подстроку “Less Then 3”.\n"
- "4 - Найти в каждом предложении строку максимальной длины, которая начинается и заканчивается цифрой. Вывести найденные подстроки по убыванию длины подстроки.\n"
- "5 - Выход из программы.\n");
- // //считывание переменной, отвечающей за выбор функции, которую нужно исполнить
- scanf("%d", &option);
- switch(option){
- case 1:
- count = del_even(initial_txt, txt_rmvd, count);
- break;
- case 2:
- sorting(initial_txt, count);
- break;
- case 3:
- less_then_3(initial_txt, &count);
- break;
- case 4:
- dig_str(initial_txt, count);
- break;
- case 5:
- printf("Программа завершена досрочно");
- return 0;
- default:
- printf("Данные некорректны");
- }
- if (option == 1){
- for(int g = 0; g < count; g++){
- printf("%s ", txt_rmvd[g]);
- }
- }
- return 0;
- }
- //удаление одинаковых предложений
- int del_eq_sen(char ** main, char**main_update, int count) {
- int k, z, w = 0;
- char*comp_1;
- char*comp_2;
- for (int i = 0; i < count; i++){
- k = 0;
- comp_1 = main[i];
- for (z = 0; z < count-1; z++) {
- if (z == i)
- z++;
- if (strlen(main[i]) == strlen(main[z])) {
- comp_2 = main[z];
- for (int j = 0; j < strlen(main[i]); j++){
- comp_1[j] = (char)(tolower(comp_1[j]));
- comp_2[j] = (char)(tolower(comp_2[j]));
- }
- if (strcmp(comp_1, comp_2) == 0) {
- k++;
- }
- }
- }
- if (i != count) {
- if (k == 0)
- {
- main_update[w] = main[i];
- w++;
- }
- }
- }
- return w;
- }
- //функция по удалению четных предложений с четным количеством слов
- int del_even(char** first_text, char** new_text,int count){
- int j; int i; int counter;
- int g = 0;
- for(j = 0; j < count; j++){
- i = 0; counter = 0;
- if(j%2 != 0){
- while(first_text[j][i] != '.'){
- if(first_text[j][i] == ' ' || first_text[j][i] == ','){
- if(first_text[j][i] == ','){
- counter++;
- i++;
- }
- else{
- counter++;
- }
- }
- i++;
- }
- counter++;
- if(counter%2 != 0){
- new_text[g] = first_text[j];
- g++;
- }
- }
- else{
- new_text[g] = first_text[j];
- g++;
- }
- }
- return g;
- }
- //функция, заменяющая слова в предложении на подстроку Less than 3
- void less_then_3(char **txt, int *count){
- for (int i = 0; i < *count; i++) {
- char* string = txt[i];
- char* new_string = (char*)malloc(1 * sizeof(char));
- new_string[0] = '\0';
- int len_string = 0;
- int len_word = 0;
- int len_new_string = 0;
- while (string[len_string] != '\0') {
- if ((string[len_string] == ',') || (string[len_string] == '.') || (string[len_string] == ' ')) {
- if (len_word <= 3) {
- new_string = (char*)realloc(new_string, (strlen(new_string) + 13) * sizeof(char));
- strcat(new_string, "Less Then 3");
- new_string[strlen(new_string) + 1] = '\0';
- new_string[strlen(new_string)] = string[len_string];
- len_new_string += 12;
- }
- else {
- new_string = (char*)realloc(new_string, (strlen(new_string) + len_word + 2) * sizeof(char));
- for (int j = 0; j <= len_word; j++) {
- new_string[len_new_string] = string[len_string - len_word + j];
- len_new_string++;
- }
- new_string[len_new_string] = '\0';
- }
- len_word = -1;
- }
- len_word++;
- len_string++;
- }
- txt[i] = new_string;
- }
- for(int s = 0; s < *count; s++)
- printf("%s ",txt[s]);
- }
- char **sentence(char *sent, int *count){
- char *istr = sent;
- int id = 0;
- char **senten = malloc (sizeof(char *));
- istr = strtok(istr," ,.");
- while (istr != NULL)
- {
- senten[id++] = istr;
- senten = realloc(senten, (id + 10) * sizeof(char));
- istr = strtok (NULL," ,.");
- }
- *count = id;
- return senten;
- }
- //функция 2
- int sup_func(const void* a, const void* b){
- const char** str_1 = (const char**)a;
- const char** str_2 = (const char**)b;
- int sum_1 = 0, sum_2 = 0, i;
- for(i = 0; i < strlen(str_1[0]); ++i)
- if(str_1[0][i] == toupper(str_1[0][i]))
- sum_1 += 1;
- for(i = 0; i < strlen(str_2[0]); ++i)
- if(str_2[0][i] == toupper(str_2[0][i]))
- sum_2 += 1;
- if(sum_1 != sum_2)
- {
- if(sum_1 > sum_2)
- return 1;
- if(sum_2 > sum_1)
- return -1;
- }
- return 0;
- }
- void sorting(char** answer, int car){
- char** dop_arr;
- char *istr;
- char ** demo;
- int count;
- for(int j = 0; j < car; j++){
- count = 0;
- dop_arr = sentence(answer[j], &count);
- qsort(dop_arr, count, sizeof(char*), sup_func);
- for(int v = 0; v < count; v++){
- printf("%s ", dop_arr[v]);
- }
- printf("\n");
- }
- }
- //функция по нахождению наибольшей строки в предложении
- void dig_str(char** text, int count){
- char** mass_words = (char**)malloc(count*sizeof(char*));
- int* lens = (int*)malloc(count * sizeof(int*));
- // int first = -1;
- // int last = -1;
- for (int i = 0; i < count; i++) {
- int first = -1;
- int last = -1;
- for (int j = 0; j < strlen(text[i]); j++) {
- if (isdigit(text[i][j])) {
- first = j;
- break;
- }
- }
- for (int k = strlen(text[i]) - 1; k >= 0; k--) {
- if (isdigit(text[i][k])) {
- last = k;
- break;
- }
- }
- /*if (first != last) {
- printf("%d %d. ", first, last);
- }
- else {
- printf("%d. ", first);
- }
- */
- if(first != -1){
- mass_words[i] = (char*)malloc((last - first + 1)*sizeof(char*));
- for (int h = first; h <= last; h++) {
- mass_words[i][h-first] = text[i][h];
- }
- lens[i] = last - first + 1;
- }
- else{
- lens[i] = 0;
- continue;
- }
- }
- for (int kus = 0; kus < count - 1; kus++) {
- for (int gus = kus + 1; gus < count; gus++) {
- if (lens[kus] < lens[gus]) {
- char* buf = mass_words[kus];
- mass_words[kus] = mass_words[gus];
- mass_words[gus] = buf;
- lens[kus] += lens[gus];
- lens[gus] = lens[kus] - lens[gus];
- lens[kus] -= lens[gus];
- }
- }
- }
- printf("\n");
- for (int l = 0; l < count; l++) {
- for(int i = 0; i < lens[l]; i++)
- putchar(mass_words[l][i]);
- printf("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement