Challenge 38

🚫

Banned Words

Open in Code Editor or Copy Instructions
...solutions at bottom of page...

A school is creating a website. To keep their site family-friendly, they have decided on a list of banned words. There are such word lists online, like this

User should be able to post comments, but their comments will be rejected if containing any of the banned words

For our example, let's imagine the following fruits are considered 'bad' words

apple
banana
cherry
date
eggplant
    

Examples:

checkComment("Giraffes eat leaves and cows eat grass") would return "Comment accepted"

checkComment("Deers eat apples and monkeys eat bananas") would return "Comment rejected - banned word(s) detected: apple, banana"

How you handle matching is your choice - in this case, if you search for partial rather than exact matches, words like "update" and "dated" would also be blocked...but for actual 'bad' words, it is unlikely that a normal word will contain it

Extension:

If the comment is successful, write it to a file, simulating it being written to a database/webpage

Solutions

IGCSE 0478 / O-Level 2210 A-Level 9618