-1

My assignment is as follows:

  1. Add a naughty word to the list
  2. Show all naughty words in the list
  3. Input sentence needing to be censored

I need to save naughty words in a file and compare if the user input contains a bad word, so i can censor it with stars. My problem is I can't seem to figure out why my method of comparing each word in the sentence to the "naughtyList.txt" isn't working. My strcmp code in OptionThree() doesn't work for a full sentence.

Here are my 3 functions:

void OptionOne() {
    char word[20];
    //"a" for continue writing in the same file, and not overwrite it
    FILE *list = fopen("naughtyList.txt", "a");                                                         
    
    printf("Enter new naughty word: ");
    scanf("%s", &word);
    
    fputs(word, list);
    fputs("\n", list);
    
    fclose(list);
}

void OptionTwo() {
    FILE *list = fopen("naughtyList.txt", "r");
    char word[20];
    
    if(list == NULL) {
        printf("Naughty words list does not exist, add some word in the list first\n");
    }else {
        printf("All naughty words in the list: \n");
        while(fgets(word, sizeof(word), list)) {
            printf("%s", word);
        }
    }
    
    fclose(list);
}

void OptionThree() {
    FILE *list = fopen("naughtyList.txt", "r");
    char word[20];
    
    char input[100];
    char eachWord[20][20];
    char *pointer;
    int i = 0;
    int count = 0;
    
    printf("Input your sentence: ");
    fflush(stdin);                                                                                      
    fgets(input, sizeof(input), stdin);
    
    //solution for getting each word in a string using strtok
    for(pointer = strtok(input, " "); pointer != NULL; pointer = strtok(NULL, " ")) {                   
        strcpy(eachWord[i], pointer);
        i++;
        count++;
    }

    for(i = 0; i < count; i++) {
        while(fgets(word, sizeof(word), list)) {
            word[strcspn(word, "\n")] = 0;  // remove tralling \n at the end of naughty word list when scaning file                                                         
            if(strcmp(eachWord[i], word) == 0) {
                printf("PING\n");
            }
        }
    }

    fclose(list);
}
  • "Doesn't work" is never a good problem description. Please give the exact input, expected result and actual result. Also, have you actually made an attempt to debug the problem? Run your program in a debugger and step thru it examining the flow and variable values to find out where things first start going wrong. For example, does `eachWord` contain the strings you exepect? Does `word` have the expected string for each iteration? etc. – kaylum Jul 22 '22 at 08:07
  • 2
    Don't do `fflush(stdin)`, it's undefined behaviour. – Sourav Ghosh Jul 22 '22 at 08:08
  • 1
    You need to `rewind` after the `fgets` loop. Better still you could read from the file into an array of strings once and not have to re-read from the file for every input word. – kaylum Jul 22 '22 at 08:08

1 Answers1

0

This should be all you need for 'option3'... I hope it is educational...

char input[100];
printf( "Input your sentence: " );
fgets( input, sizeof(input), stdin );
input[ strlen( input ) - 1 ] = 0;

FILE *fp = fopen( "naughtyList.txt", "r" );
if( fp == NULL ) exit(0); // handle error...

char word[20];
while( fgets( word, sizeof( word ), fp ) ) {
    word[ strlen( word ) - 1 ] = 0;

    char *cp;
    while( ( cp = strstr( input, word ) ) != NULL )
        for( char *xp = word; *xp; xp++ )
            *cp++ = '#';
}
fclose( fp );

printf( "Clean: '%s'\n", input );
Fe2O3
  • 6,077
  • 2
  • 4
  • 20