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>
- int del_eq_sen(char** main, char** main_update, int count);
- int del_even(char** first_text, char** new_text, int count);
- char ** plus(const void *a, const void* b);
- char **sentence(char *sent, int *count);
- char** strt_dig(char** txt_1, int * counter, int count);
- int comparator(const void* word1, const void* word2);
- void less_then_3(char **txt, int *count);
- int main(){
- char* input_words;
- char** initial_txt;
- char** main_upd;
- char** txt_rmvd;
- initial_txt = malloc(100*sizeof(char*));
- char symb;
- int n;
- //выделение памяти под строку с текстом
- 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);
- }
- }
- //выделение памяти нп массив указателей (предложения)
- 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);
- initial_txt[count][j] = '\0';
- count++;
- if (count == size_arr - 1){
- size_arr = size_arr*2;
- initial_txt = realloc(initial_txt, size_arr);
- }
- 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:
- //// qsort(initial_txt, count, sizeof(char*), comparator);
- //// for(int i = 0; i < count; i++){
- //// printf("%s", main_update[i]);
- //// }
- //
- // break;
- case 3:
- less_then_3(initial_txt, &count);
- break;
- case 4:
- txt_rmvd = strt_dig(initial_txt, &counterslova, count);
- count = counterslova;
- break;
- // abcb2 1a2. abcde 1abc2. 1def.
- case 5:
- printf("Программа завершена досрочно");
- return 0;
- default:
- printf("Данные некорректны");
- }
- 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 amount = count;
- 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){
- // free(first_text[j]);
- // amount--;
- new_text[g] = first_text[j];
- g++;
- }
- }
- else{
- new_text[g] = first_text[j];
- g++;
- }
- }
- return g;
- }
- //
- void less_then_3(char **txt, int *count){
- // char** updatedtxt=malloc(*count_sent*sizeof(char));
- // char** new_text = txt;
- 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]);
- }
- //gh gfyet. grwrwrh. das ada. daddad adad. 12rt erstt.
- // int comparator(const void* word1, const void* word2){
- // char *x = * word1;
- // char *y = * word2;
- // for
- // }
- // return
- // }
- // char** mass_words(char ** t_text, int count, func){
- // char ** t_words = malloc(sizeof(char*));
- // int i;
- // int kolvo;
- // for(i = 0; i < count; i++){
- // t_words = func(t_text[i], &kolvo);
- // }
- // }
- //функция по нахождению наибольшей строки в предложении
- char** strt_dig(char** txt_1, int * counter, int count){
- int k, j;
- int i;
- int numbs = 0;
- char** dop_arr = NULL;
- char** slova = malloc(sizeof(char*));
- for(i = 0; i < count; i++){
- char* oneword = NULL;
- int amount = 0;
- dop_arr = sentence(txt_1[i], &amount);
- // printf("%s ", dop_arr[0]);
- for (k = 0; k < amount; k++){
- if(isdigit(dop_arr[k][0]) && isdigit(dop_arr[k][strlen(dop_arr[k])-1])){
- if(oneword == NULL){
- oneword = dop_arr[k];
- // printf("%s ", dop_arr[k]);
- }
- else{
- if(strlen(oneword) <= strlen(dop_arr[k])){
- oneword = dop_arr[k];
- }
- }
- }
- }
- slova[numbs++] = oneword;
- slova = realloc(slova, (numbs+1)*sizeof(char*));
- }
- for(int kus = 0; kus < numbs - 1; kus++){
- for(int gus = kus + 1; gus < numbs; gus++){
- if(strlen(slova[kus]) < strlen(slova[gus])){
- char* buffer = slova[kus];
- slova[kus] = slova[gus];
- slova[gus] = buffer;
- }
- }
- }
- *counter = numbs;
- return slova;
- }
- //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.
- 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 + 1) * sizeof(char));
- istr = strtok (NULL," ,.");
- }
- *count = id;
- return senten;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement