Click here to Skip to main content
15,886,776 members

Coding challenge: bad word filter

Chris Maunder asked:

Open original thread
This is the first of our weekly coding challenges. Once a week we'll post a simple programming problem and the person who posts the best answer wins a T-shirt. The winner is decided by votes, or be comments, or be the audacity of their answer. Use any language in your answer except profanity.

Imagine that you allow users of your website to post comments, but some users, mainly the Australians, get a little colourful with their language. You decided to implement a Bad Word Filter that will replace certain words in a sentence with safer versions of that word.

The list of bad words and their replacements is

Bad: "poophead", "PHB", "gotten"
Replacement: "p**phead", "boss", "become"

So "My PHB is such a poophead. It's gotten worse since his promotion" should be "My boss is such a p**phead. It's become worse since his promotion".

We also have to allow shouting. So

"My PHB is such a POOPHEAD!" should become "My boss is such a P**PHEAD!"

Bonus points:

Let's make it harder. If the "bad" word starts with "*" then it means any word that ends with that word. If it ends with a star then any word starting with that. If it ends with an "!" then it means that it should do the match case sensitive.

Bad words: "poop*", "PHB!", "gotten"
Replacement: "p**p", "boss", "become"

"My PHB has started his new blog phblog.com. He's SUCH A MISBEGOTTEN POOPHEAD!"

should become

"My boss has started his new blog phblog.com. He's SUCH A MISBEGOTTEN P**PHEAD!"

What I have tried:

Remember: any programming language can be used.
Tags: Code challenge

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900