How to check whether a user string contains any forbidden words from a list in C

2 Answers

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

static const char *forbidden_sorted[] = {
    "badword",
    "evil",
    "kill",
    "nasty",
    "terrible"
};

static const size_t forbidden_words_size =
    sizeof(forbidden_sorted) / sizeof(forbidden_sorted[0]);

int cmpstr(const void *a, const void *b) {
    const char *key   = *(const char * const *)a;
    const char *entry = *(const char * const *)b;
    return strcmp(key, entry);
}

int contains_forbidden(const char *input) {
    char buffer[1024];
    strncpy(buffer, input, sizeof(buffer) - 1);
    buffer[sizeof(buffer) - 1] = '\0';

    char *token = strtok(buffer, " \t\n\r.,!?;:-()[]{}\"'");

    while (token) {
        // Normalize to lowercase
        for (char *p = token; *p; p++)
            *p = (char)tolower((unsigned char)*p);

        const char *key = token;

        if (bsearch(&key, forbidden_sorted, forbidden_words_size,
                    sizeof(char *), cmpstr)) {
            return 1;
        }

        token = strtok(NULL, " \t\n\r.,!?;:-()[]{}\"'");
    }

    return 0;
}

int main() {
    const char *s = "This text contains a badword inside";

    if (contains_forbidden(s)) {
        puts("Forbidden word detected");
    } else {
        puts("No forbidden words found");
    }
    return 0;
}



/*
run:

Forbidden word detected

*/

 



answered Dec 26, 2025 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

static const char *forbidden_sorted[] = {
    "badword",
    "evil",
    "kill",
    "nasty",
    "terrible"
};

static const size_t forbidden_words_size =
    sizeof(forbidden_sorted) / sizeof(forbidden_sorted[0]);

// Fixed comparator: bsearch passes a pointer to the key (char**) 
// and a pointer to the element in the array (char**)
int cmpstr(const void *a, const void *b) {
    const char *key = *(const char **)a;
    const char *entry = *(const char **)b;
    
    return strcmp(key, entry);
}

int contains_forbidden(const char *input) {
    char word[256];
    const char *word_ptr = word; // We need a pointer to the key for bsearch
    size_t i = 0;

    while (1) {
        // Process character if it's alphanumeric, otherwise handle word boundary
        if (*input && isalnum((unsigned char)*input)) {
            if (i < sizeof(word) - 1)
                word[i++] = (char)tolower((unsigned char)*input);
        } else {
            if (i > 0) {
                word[i] = '\0';
                
                // Pass pointer to the pointer to the string
                if (bsearch(&word_ptr, forbidden_sorted, forbidden_words_size,
                            sizeof(char *), cmpstr)) {
                    return 1;
                }
                i = 0;
            }
            if (!*input) break; // End of string reached
        }
        input++;
    }

    return 0;
}

int main() {
    const char *s = "This text contains a badword inside";

    if (contains_forbidden(s)) {
        puts("Forbidden word detected");
    } else {
        puts("No forbidden words found");
    }
    
    return 0;
}



/*
run:

Forbidden word detected

*/

 



answered Dec 26, 2025 by avibootz

Related questions

...