Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <locale.h>
- char *readSentence();
- char **readText(char **Text, int *Count_sentence);
- char **DeleteNumbers(char **Text, int *Count_sentence);
- void OutputisPalindrome(char **Text, int *Count_sentence);
- char **DeleteSentence(char **Text, int *Count_sentence);
- char **SortbyLengthoftheThirdWord(char **Text, int *Count_sentence);
- int main(){
- setlocale(LC_ALL,"");
- char** Text = (char**)malloc(sizeof(char*));
- int Count_sentence = 0;
- Text = readText(Text, &Count_sentence);
- int choise;
- do{
- wprintf(L"Выбор функции:\nЧтобы удалить все встречающиеся в предложениях цифры, нажмите 1\nЧтобы найти предложения-палиндромы, нажмите 2\nЧтобы удалить все предложения, в которых совпадает первый и последний символ(без учета регистра), нажмите 3\nЧтобы отсортировать предложения по увеличению длины третьего слова, нажмите 4\nЧтобы выйти из программы, нажмите 0\n");
- scanf("%d",&choise);
- switch(choise){
- case 1:
- Text = DeleteNumbers(Text, &Count_sentence);
- break;
- case 2:
- OutputisPalindrome(Text, &Count_sentence);
- break;
- case 3:
- Text = DeleteSentence(Text, &Count_sentence);
- break;
- case 4:
- Text = SortbyLengthoftheThirdWord(Text, &Count_sentence);
- break;
- case 0:
- printf("Goodbye");
- break;
- default:
- wprintf(L"Данные некорректны");
- }
- }while(choise != 0);
- for (int i = 0; i < Count_sentence; i++){
- //printf("%s\n",Text[i]);
- free(Text[i]);
- }
- free(Text);
- return 0;
- }
- char **readText(char **Text,int *Count_sentence){
- int size = 10; //память в text
- char *sentence;
- do{
- sentence = readSentence();
- if (*Count_sentence == size -1){
- size += 10;
- Text = realloc(Text, size * sizeof(char*));
- }
- Text[*Count_sentence] = sentence;
- (*Count_sentence)++;
- }while(strcmp(sentence,"End!"));
- return Text;
- }
- char *readSentence(){
- char *sentence = malloc(10*sizeof(char));
- char current_symbol;
- int Count_symbol = 0; //символ в предложении
- int size_sentence = 10; //память в предложении
- do{
- current_symbol = getchar();
- if ((current_symbol == ' ' || current_symbol == '\t') && !Count_symbol){
- current_symbol = getchar();
- }
- if (Count_symbol == size_sentence - 2 ){
- size_sentence += 10;
- sentence = realloc(sentence,size_sentence*sizeof(char));
- }
- sentence[Count_symbol] = current_symbol;
- Count_symbol++;
- }while ((current_symbol != '\n')&&(current_symbol != '.')&&(current_symbol !='!'));
- sentence[Count_symbol] = '\0';
- return sentence;
- }
- char **DeleteNumbers(char **Text, int *Count_sentence){
- int i = 0;
- while (i < *Count_sentence){
- for (int j = 0; j < strlen(Text[i]); j++){
- if (isdigit(Text[i][j])){
- if (Text[i][j+1] == ' ')
- for (int s = j; s < strlen(Text[i])-1; s++){
- //printf("%d: %c %c\n",s,Text[i][s],Text[i][s+2]);
- Text[i][s] = Text[i][s+2];
- }
- else
- {
- for (int s = j; s < strlen(Text[i]); s++){
- //printf("%d: %c %c\n",s,Text[i][s],Text[i][s+1]);
- Text[i][s] = Text[i][s+1];
- }
- }
- j--;
- }
- }
- if (strlen(Text[i]) == 1){
- int sen;
- for (sen = i; sen < *Count_sentence - 1; sen++){
- Text[sen] = Text[sen+1];
- }
- free(Text[sen+1]);
- (*Count_sentence)--;
- }
- else{
- i++;
- }
- }
- for (int i = 0; i < *Count_sentence; i++){
- printf("%s\n",Text[i]);
- //free(Text[i]);
- }
- //free(Text);
- //return Text;
- }
- void OutputisPalindrome(char **Text, int *Count_sentence){
- int i = 0;
- while (i < *Count_sentence){
- int end = strlen(Text[i]) - 2;
- int start = 0;
- while (start != end){
- if (Text[i][start] == ' ')
- start++;
- if(Text[i][end] == ' '){
- end--;
- if (Text[i][end] == ',')
- end--;
- }
- if (Text[i][start] == ',')
- start += 2;
- if (Text[i][start] == Text[i][end]){
- start++;
- end--;
- }
- else{
- printf("%s - Nothing interesting\n", Text[i]);
- break;
- }
- if (start >= end){
- printf("%s - Palindrome\n", Text[i]);
- break;
- }
- }
- i++;
- }
- /*for (int i = 0; i < *Count_sentence; i++){
- free(Text[i]);
- }
- free(Text);*/
- }
- char **DeleteSentence(char **Text, int *Count_sentence){
- int i = 0;
- int s;
- while (i < *Count_sentence){
- int j = strlen(Text[i]) - 2;
- if (tolower(Text[i][0])==tolower(Text[i][j])){
- for (s = i; s < *Count_sentence - 1; s++){
- Text[s] = Text[s+1];
- }
- free(Text[s+1]);
- (*Count_sentence)--;
- }
- else{
- i++;
- }
- }
- for (int i = 0; i < *Count_sentence; i++){
- printf("%s\n",Text[i]);
- //free(Text[i]);
- }
- //free(Text);
- }
- int compare (const void *i, const void *j){
- if(*(int*)i > *(int*)j)
- return 1;
- else if (*(int*)i == *(int*)j)
- return 0;
- else
- return -1;
- }
- char **SortbyLengthoftheThirdWord(char **Text, int *Count_sentence){//как-то работает
- char **sortText = malloc((*Count_sentence)*sizeof(char));
- int i = 0;
- int lenThirdWorld[*Count_sentence];
- int *sortlenThirdWorld = (int*)calloc((*Count_sentence),sizeof(int));
- for (int i = 0; i < *Count_sentence; i++){
- lenThirdWorld[i] = 3;
- sortlenThirdWorld[i] = 3;
- }
- while (i < *Count_sentence){
- int numSpace = 0;
- for (int j = 0; j < strlen(Text[i]); j++){
- if (Text[i][j] == ' '){
- numSpace += 1;
- }
- if (numSpace == 2){
- int s = j + 1;
- lenThirdWorld[i] = 0;
- sortlenThirdWorld[i] = 0;
- while (Text[i][s] != ' ' && Text[i][s] != ',' && Text[i][s] != '.'){
- lenThirdWorld[i] += 1;
- sortlenThirdWorld[i] += 1;
- s++;
- }
- break;
- }
- }
- i++;
- }
- qsort(sortlenThirdWorld, *Count_sentence, sizeof (int), compare);
- int count = 0;
- for (int k = 0; k < *Count_sentence; k++){
- for (int p = 0; p < *Count_sentence; p++){
- if (Text[p] != NULL){
- if (lenThirdWorld[p] == sortlenThirdWorld[k]){
- sortText[count] = Text[p];
- Text[p] = NULL;
- count += 1;
- break;
- }
- }
- }
- }
- for (int i = 0; i < *Count_sentence; i++)
- free(Text[i]);
- /*free(Text);
- for (int i = 0; i < *Count_sentence; i++){
- printf("%s\n", sortText[i]);
- free(sortText[i]);
- }
- free(sortText);*/
- return sortText;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement