Experiment 12_A_ Harmonious vocabulary

Title Description

There are many discordant words in the Internet. When we browse the Internet, search engines often appear the prompt message "according to relevant laws, regulations and policies, some search results are not displayed". Xiao Wang also wants to make his own shielded thesaurus, so that he can replace the words he doesn't like from the file with other characters. The shielded thesaurus is an ASCII file. This file only contains words, each word occupies one line, and each word may only have upper and lower case letters and spaces. The file name of the harmonious thesaurus in the title is dict.dic. (the length of each word in the shielded thesaurus shall be less than 10, and the number of shielded words shall not exceed 10.)
Your task is to replace all the words in the harmonious thesaurus with "@# %^&*"(press and hold the keyboard shift and the numbers 1 to 8), and then output. It should be noted here that if a word contains shielded words, only the shielded words will be replaced. For ex amp le, after "hehasAAA" is processed, it will get "he@# %^&*AAA ", note that the shielded words are case sensitive, that is, AAA and AAA are two different words. In order to simplify the problem, there will be no mutual inclusion in the shielded words, such as" xabcx "and" abc "will not appear in the same shielded thesaurus at the same time. Because Xiao Wang is not good at file operation, he hopes you can help him make this program to block vocabulary.

input

Several strings less than 110 in length.

output

Results after adding shielding words.

sample input

The night falls gently. And you are not here. I missing you more and more and I start getting worried as
I stare at the door just waiting for you to surprise me with your arrival at any moment.
Sweet delusion... you are so far away right now that all I can ask for is that time moves faster...

sample output

If the shielded thesaurus is as follows: (there is a newline character in each line of the file)
is
good
are
the
ha ha
some
get
has
more
bad

Output:

 The night falls gently. And you !@#$%^&* not here. I m!@#$%^&*sing you !@#$%^&* and !@#$%^&* and I start !@#$%^&*ting worried as
I st!@#$%^&* at !@#$%^&* door just waiting for you to surpr!@#$%^&*e me with your arrival at any moment.
Sweet delusion... you !@#$%^&* so far away right now that all I can ask for !@#$%^&* that time moves faster...

source code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void restart(int *,int *);
int main(){
    FILE * fp = fopen("dict.dic","r");
    char dictionary[15][15] = {0};
    char line[400] = {0};
    int i = 0,j = 0;
    char ch;
    int start[111],end[111];
    restart(start,end);
    while(!feof(fp)){
        ch = (char)fgetc(fp);
        while(ch != '\n' && ch != EOF){
            dictionary[i][j] = ch;
            ch = (char)fgetc(fp);
            j++;
        }
        j = 0;
        i++;
    }
    gets(line);
    while(line[0] != 0){
    	int k,l = 0;
        int flag1 = 1,flag2;
        for(i = 0;i < 400 && flag1;){
            int temp1 = i;
            if(line[i] == 0) flag1 = 0;
            else{
            	flag2 = 1;
                for(j = 0;j < 11 && flag2;j++){
                    int on = i;
                    while(line[i] == dictionary[j][k] && line[i]){
                        k++;
                        i++;
                    }
                    if(dictionary[j][k] == 0 && dictionary[j][0] != 0){
                        start[l] = on;
                        end[l] = i;
                        l++;
                        k = 0;
                        flag2 = 0;
                    }
                    else{
                        i = on;
                        k = 0;
					}
                }
            }
            if(flag2){
            	i = temp1;
            	i++;
			}
        }
        j = 0;
        for(i = 0;i <400 && line[i];i++){
            while((i < start[j]) && i < 400 && line[i]){
                putchar(line[i]);
                i++;
            }
            if(start[j] != 200) printf("!@#$%^&*");
            i = end[j]-1;
            j++;
        }
        printf("\n");
        restart(start,end);
        memset(line,0,400);
        gets(line);
    }
    return 0;
}
void restart(int * start,int * end){
    int i;
    for(i = 0;i < 111;i++){
        start[i] = 200;
        end[i] = 200;
    }
}

lesson

Originally, a more complex algorithm using linked list was used for the complexity of the algorithm. It's much better after changing the array. It's solved directly by violence. In the future, we should pay attention to the requirements of the problem, and those who can solve by violence will be solved directly by violence

Keywords: C

Added by roughie on Tue, 08 Mar 2022 01:42:08 +0200