Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <locale.h>
- const int MAX_SIZE = 256;
- char** text;
- char* separ;
- char** word;
- int count_str, count_separator, count_same_words;
- void GetText()
- {
- char tmp[MAX_SIZE];
- char** temp;
- int i;
- int end_input;
- end_input=0;
- count_str = 0;
- temp=NULL;
- printf("Text:\n");
- fflush(stdin);
- for (i=0; !end_input; i++)
- {
- gets(tmp);
- free(temp);
- temp=(char**)malloc((count_str+1)*sizeof(char*));
- int j;
- for(j=0;j<count_str;j++)
- {
- temp[j]=(char*)malloc((strlen(text[j])+1)*sizeof(char));
- strcpy(temp[j],text[j]);
- }
- temp[i]=(char*)malloc((strlen(tmp)+1)*sizeof(char));
- strcpy(temp[i],tmp);
- count_str++;
- text=(char**)malloc(count_str*sizeof(char*));
- for(j=0;j<count_str;j++)
- {
- text[j]=(char*)malloc((strlen(temp[j])+1)*sizeof(char));
- strcpy(text[j],temp[j]);
- }
- for(j=0;j<count_str-1;j++)
- {
- if(strcmp(text[j],text[i])==0)
- {
- int k;
- for(k=0;k<count_str;k++) free(temp[k]); free(temp);
- end_input = 1;
- break;
- }
- }
- }
- }
- void GetSeparator()
- {
- puts("Size of separators:");
- scanf("%d%*c",&count_separator);
- fflush(stdin);
- separ = (char*)malloc((count_separator+2)*sizeof(char));
- gets(separ);
- separ[count_separator]='\n';separ[count_separator+1]='\0';
- }
- void OutSep()
- {
- printf("Separators:\n");
- printf("%s", separ);
- fflush(stdin);
- }
- void OutText()
- {
- printf("Text:\n");
- int i;
- for (i = 0; i < count_str; i++)
- printf("%s\n", text[i]);
- fflush(stdin);
- getchar();
- }
- char* CountSeparators(char* str, int *count)
- {
- int result,i;
- result=0;
- for (i=0; i<strlen(str); i++)
- if ( strchr(separ, str[i]) != NULL)
- result++;
- *count = result;
- return str;
- }
- char* CheckStr(char* stroka, int *how)
- {
- int count_word;
- int result;
- result = 0;
- count_word=0;
- stroka = CountSeparators(stroka, &count_word);
- word = (char**)malloc( (count_word+1) * sizeof(char*));
- char *tmp;
- tmp = strtok(stroka, separ);
- int i,j;
- i=0;
- while ( tmp != NULL)
- {
- word[i] = (char*)malloc( (strlen(&tmp[0])+1) * sizeof(char));
- strcat(word[i], tmp);
- word[i][strlen(&word[i][0])+1] = '\0';
- word[i][strlen(&word[i][0])]=' ';
- //printf("%s\n", word[i]);
- i++;
- tmp = strtok(NULL, separ);
- }
- int count_same;
- count_same=0;
- for (i=0;i<count_word; i++)
- {
- int t;
- t=1;
- for (j=i+1; j<count_word; j++)
- {
- if ( strcmp(word[i], word[j]) == 0)
- t++;
- }
- if ( t> count_same)
- count_same=t;
- }
- if ( count_same > count_same_words )
- {
- int max_len;
- max_len=0;
- for (i=0; i<count_word; i++)
- if ( strlen(&word[i][0]) > max_len)
- max_len= strlen(&word[i][0]);
- for (i=0; i<count_word; i++)
- if ( strlen(&word[i][0]) == max_len)
- {
- free(word[i]);
- }
- }
- free(stroka);
- stroka = (char*)malloc( sizeof(word));
- for (i=0; i<count_word; i++)
- if ( word[i] != NULL)
- {
- strcat(stroka, word[i]);
- result=1;
- }
- else free(word[i]);
- free(word);
- *how=result;
- return stroka;
- }
- char** OperateText(char** text)
- {
- int i;
- return text;
- }
- int main()
- {
- setlocale(LC_ALL, "rus");
- char inpChar='1';
- while(inpChar!='0')
- {
- printf("1) Ввод текста\n");
- printf("2) Ввод разделителей\n");
- printf("3) Ввод количества повторяющихся слов\n");
- printf("4) Обработка текста\n");
- printf("5) Вывести текст\n");
- printf("6) Вывод разделителей\n");
- printf("0) Выход\n\n");
- printf("Выбор:\t");
- inpChar = getchar();
- while ( inpChar == '\n') inpChar = getchar();
- fflush(stdin);
- switch(inpChar)
- {
- case '1':
- {
- GetText();
- break;
- }
- case '2':
- {
- GetSeparator();
- break;
- }
- case '3':
- {
- fflush(stdin);
- puts("Количество повторяющихся слов:");
- scanf("%d%*c",&count_same_words);
- fflush(stdin);
- break;
- }
- case '4':
- {
- int i;
- for (i=0; i<count_str; i++)
- {
- int flag;
- flag=0;
- //printf("%s\n", text[i]);
- text[i] = CheckStr(text[i], &flag);
- //printf("%s\n", text[i]);
- if ( !flag )
- {
- int j;
- for (j=i+1; j<count_str; j++)
- {
- free(text[j]);
- text[j-1] = (char*)malloc( (strlen(text[j])+1) * sizeof(char) );
- text[j-1] = text[j];
- }
- count_str--;
- i--;
- }
- }
- break;
- }
- case '5': OutText(); break;
- case '6': OutSep(); break;
- case '0': break;
- default:
- {
- printf("Неверно выбранный пункт! Повторите снова\n");
- break;
- }
- }
- }
- if(text!=NULL)
- {
- int i;
- for (i = 0; i < count_str; i++)
- free(text[i]);
- free(text);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment