Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- #include <ctype.h>
- #define N 10
- #define M 100
- char *scan_sentences(){
- char *s = malloc(N*sizeof(char));
- int counter = 0;
- int size = N;
- char current_symbol;
- current_symbol = getchar();
- while ((current_symbol == ' ') || (current_symbol == '\t')){
- current_symbol = getchar();
- }
- while ((current_symbol!= '\n') && (current_symbol != '.') && (current_symbol != '!')){
- if(counter>=size - 5){
- size+=N;
- s = realloc(s, size);
- }
- s[counter] = current_symbol;
- counter++;
- current_symbol = getchar();
- }
- s[counter++] = current_symbol;
- s[counter++] = '\n';
- s[counter] = '\0';
- return s;
- }
- bool stcp(char* a, char* b){
- int lena = strlen(a);
- int lenb = strlen(b);
- char* newa = calloc(lena+1, sizeof(char));
- char* newb = calloc(lenb+1, sizeof(char));
- for(int i = 0; i<lena; i++){
- newa[i]=toupper(a[i]);
- if(i==lena-1){
- i++;
- newa[i]='\0';
- }
- }
- for(int i = 0; i<lenb; i++){
- newb[i]=toupper(b[i]);
- if(i==lenb-1){
- i++;
- newb[i]='\0';
- }
- }
- int st = strcmp(newa,newb);
- free(newa);
- free(newb);
- return(st==0) ? true : false;
- }
- char** find_simmilar_and_kill(char** pointers, int* pointonsizeofmassive){
- const unsigned int sizeofmassive = *(pointonsizeofmassive);
- bool *boolmask1 = malloc(sizeofmassive*sizeof(bool));
- for(int i = 0; i<sizeofmassive; i++){
- boolmask1[i] = true;
- }
- for(int i = 0; i<(sizeofmassive-1); i++){
- for(int j=i+1; j<sizeofmassive; j++){
- if(stcp(pointers[i],pointers[j])){
- boolmask1[j]=false;
- }
- }
- }
- int suits = 0;
- for(int i = 0; i<sizeofmassive; i++){
- if(boolmask1[i]!=false){
- suits++;
- }
- }
- char** newpointers = malloc(suits*sizeof(char*));
- int j = 0;
- for(int i=0; i<sizeofmassive; i++) {
- if (boolmask1[i]) {
- newpointers[j++] = pointers[i];
- } else {
- free(pointers[i]);
- }
- }
- free(pointers);
- free(boolmask1);
- *pointonsizeofmassive = suits;
- return newpointers;
- }
- char* do_upper(char* a) {
- int lena = strlen(a);
- char *newa = malloc((lena + 1) * sizeof(char));
- for (int i = 0; i < lena; i++) {
- if(!(a[i]>=48 && a[i]<=57)){
- if (i == 0) {
- newa[i] = toupper(a[i]);
- } else {
- newa[i] = tolower(a[i]);
- }
- }else{
- newa[i] = a[i];
- }
- if (i == lena - 1) {
- i++;
- newa[i] = '\0';
- }
- }
- return newa;
- }
- char**find_20018_and_kill(char** pointers, int *pointonsizeofmassive) {
- const int sizeofmassive =*(pointonsizeofmassive);
- char b[5] = "2018";
- bool *boolmask2 = malloc(sizeofmassive*sizeof(bool));
- for(int i = 0; i<sizeofmassive; i++){
- boolmask2[i] = true;
- }
- int doless = 0;
- for(int i = 0; i<sizeofmassive; i++){
- if(strstr(pointers[i],b)){
- doless++;
- boolmask2[i]=false;
- }
- }
- char** newpointers = malloc((sizeofmassive-doless)*sizeof(char));
- int j = 0;
- for(int i=0; i<sizeofmassive; i++) {
- if (boolmask2[i]) {
- newpointers[j++] = pointers[i];
- } else {
- free(pointers[i]);
- }
- }
- free(pointers);
- free(boolmask2);
- *pointonsizeofmassive = sizeofmassive - doless;
- return newpointers;
- }
- void print_all_numbers(char** pointers, int* pointonsizeofmassive){
- for(int i = 0; i < *(pointonsizeofmassive); i++){
- if(strstr(pointers[i], "0") && strstr(pointers[i], "1") && strstr(pointers[i], "2") && strstr(pointers[i], "3") && strstr(pointers[i], "4") && strstr(pointers[i], "5") && strstr(pointers[i], "6") && strstr(pointers[i], "7") && strstr(pointers[i], "8") && strstr(pointers[i], "9")){
- printf("%s", pointers[i]);
- }else{
- printf("There is no such a sentence.\n");
- }
- }
- }
- void justprint(char** pointers,int*pointonsizeofmassive){
- for (int i = 0; i < *pointonsizeofmassive; i++) {
- fputs(pointers[i], stdout);
- }
- }
- int compare(const void* a, const void* b)
- {
- const char* str1 = *(char**)a;
- const char* str2 = *(char**)b;
- bool isStr1HasDigit = false;
- bool isStr2HasDigit = false;
- int sum1 = 0;
- int sum2 = 0;
- for (int i = 0; str1[i]; i++)
- if(isdigit(str1[i])){
- sum1+=str1[i]-'0';
- isStr1HasDigit = true;
- }
- for (int i = 0; str2[i]; i++)
- if(isdigit(str2[i])){
- sum2+=str2[i]-'0';
- isStr2HasDigit = true;
- }
- if(!isStr1HasDigit)
- return 1;
- if(!isStr2HasDigit)
- return -1;
- return sum1-sum2;
- }
- char** sort_by_sum(char** pointers, int *pointonsizeofmassive){
- qsort(pointers,*(pointonsizeofmassive), sizeof(char*), compare);
- return pointers;
- }
- char**read_text(char**pointers, int* sizeofmassive){
- int counter = 0;
- int size = M;
- char *s;
- int sizeofmymassive = 0;
- do {
- s = scan_sentences();
- if (counter == size - 1) {
- size += M;
- pointers = realloc(pointers, size * sizeof(char *));
- }
- pointers[counter] = s;
- counter++;
- sizeofmymassive++;
- }while(strcmp(s,"\n\n")!=0);
- *(sizeofmassive) = sizeofmymassive -1;
- return pointers;
- }
- int main(){
- printf("Dear user, enter your text.\n");
- char **pointers = malloc(M*sizeof(char*));
- int sizeofmassive;
- pointers = read_text(pointers, &sizeofmassive);
- int *pointonsizeofmassive = &sizeofmassive;
- pointers=find_simmilar_and_kill(pointers, pointonsizeofmassive);
- int choise = 1;
- printf("Please, choose what you want to do with your text:\n");
- do {
- printf("1: convert the text so that the first word will start with a capital letter and others will be lowercase\n"
- "2: Delete all sentences that contain 2018\n"
- "3: sort sentences by increasing the sum of the digits\n"
- "4: print on screen sentences that contain all digits\n"
- "0: quit the programm\n");
- scanf("%d", &choise);
- if(choise!=1&&choise!=2&&choise!=3&&choise!=4&&choise!=0){
- printf("Invalid input!\n"
- "Try again.\n");
- scanf("%d", &choise);
- }
- switch (choise) {
- case 1:
- for (int i = 0; i < *pointonsizeofmassive; i++) {
- pointers[i] = do_upper(pointers[i]);
- }
- justprint(pointers, pointonsizeofmassive);
- printf("Choose again, please\n");
- break;
- case 2:
- pointers = find_20018_and_kill(pointers, pointonsizeofmassive);
- justprint(pointers, pointonsizeofmassive);
- printf("Choose again, please\n");
- break;
- case 3:
- pointers = sort_by_sum(pointers, pointonsizeofmassive);
- justprint(pointers, pointonsizeofmassive);
- printf("Choose again, please\n");
- break;
- case 4:
- print_all_numbers(pointers, pointonsizeofmassive);
- printf("Choose again, please\n");
- break;
- case 0:
- printf("See you later!");
- }
- }while (choise);
- for(int i = 0; i<*(pointonsizeofmassive);i++){
- free(pointers[i]);
- }
- free(pointers);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement