black hole sun
New member
Hi guys, I thought it might be fun to try to create a swear filter in C++. So, after many hours of labor and imported code from another project, here's what I've come up with.
Rage3d's own swear filter is messing up my results which is kind of lame, but you can use your imaginations to fill in the blanks.
The header file:
It works just great, but my question is, checkForProfanity(char * str) is kind of lame with all its if-statements, is there any way to do this better? Like, with enums or something (i've not learned enums yet so I wouldn't know...)
Rage3d's own swear filter is messing up my results which is kind of lame, but you can use your imaginations to fill in the blanks.
Code:
// censor.cpp -- 3/22/06
// replaces naughty words with euphemisms or asterisks
#include <iostream>
#include "swearEnum.h"
using namespace std;
int main(void)
{
char str[500];
cout << "Enter a string full of bad words -- I'll censor it. \n";
cin.getline(str, 499);
//check the string for profanity
checkForProfanity(str);
//print out the (now-clean) string
cout << str << endl;
return 0;
}
//search the string for swear words
void checkForProfanity(char * str)
{
if (strstr(str, "mother****er") != NULL)
delProfanity(str, "mother****er", 12, mother****er);
if (strstr(str, "mother****ing") != NULL)
delProfanity(str, "mother****ing", 13, mother****ing);
if (strstr(str, "****") != NULL)
delProfanity(str, "****", 4, ****);
if (strstr(str, "****") != NULL)
delProfanity(str, "****", 4, ****);
if (strstr(str, "damn") != NULL)
delProfanity(str, "damn", 4, damn);
if (strstr(str, "*****") != NULL)
delProfanity(str, "*****", 5, *****);
if (strstr(str, "faggot") != NULL)
delProfanity(str, "faggot", 6, faggot);
if (strstr(str, "fag") != NULL)
delProfanity(str, "fag", 3, fag);
}
/* delProfanity accepts four args:
char *string : the swear-laden user-inputted sentence;
char *srchTerm : the swear word the function looks for and replaces;
int wordCount : the word-count of the swear word;
char *replacement: a pointer to the array containing the replacement
for the swear word; usually, this array is full
of stars (e.g., '****'), but that can be changed by modifying
swearEnum.h. */
void delProfanity(char *string, char *srchTerm, int wordCount, char *replacement)
{ start:
int lenOfSwearReplacement = strlen(replacement);
char tempString[100] = {0};
int i = 0;
char *ptrToFirstLetter = strstr(string, srchTerm);
// the below pointer is the same as the above, but this
// one remains unmodified so the same memory location
// can be used later
char *permPtrToFirstLetter = strstr(string, srchTerm);
// split the sentence where the search term is
while(*ptrToFirstLetter)
{
tempString[i] = *ptrToFirstLetter;
ptrToFirstLetter++
i++;
}
int lenOfSentence = strlen(string);
int lenOfTempString = strlen(tempString);
int difference = (lenOfSentence - lenOfTempString);
// in case the search term happens to be at the end of
// a string -- in that case, the temporary string and
// the search term will be equal.
if(strcmp(tempString, srchTerm) == 0)
{
// chop off the swear
string[difference] = NULL;
// insert euphemism -- stars, usually, but it can be a replacement phrase
strcat(string, replacement);
return;
}
// chop the original string right where the temp
// string is copied.
*permPtrToFirstLetter = NULL;
// insert euphemism -- stars, usually, but it can be a replacement phrase
strcat(string, replacement);
//increment 'difference' the length of the euphemism
difference += lenOfSwearReplacement;
// now copy back the fragment in tempString[], omitting the search term.
for(int j = (wordCount); j <= lenOfTempString; j++)
{
string[difference] = tempString[j];
difference++;
}
// if there is more than one occurance of a word that needs to be deleted,
// do all the above again. much love, goto!
if(strstr(string, srchTerm))
goto start;
}
The header file:
Code:
// swear dictionary, function declarations
#ifndef swear
#define swear
void checkForProfanity(char*);
void delProfanity(char*, char*, int, char*);
// common four-letter-words -- and their euphemisms.
// use these instead of stars for some comedic value
// when the sentences are printed
/* The below have been raped by Rage's swear filter...
char ****[] = "have sex with ",
****[] = "defecate ",
damn[] = "darn ",
*****[] = "female dog ",
mother****er[] = "incestuous boy ",
mother****ing[] = "incestuous ";
*/
// current replacement table:
char fag[] = "***",
faggot[] = "******",
****[] = "****",
****[] = "****",
damn[] = "****",
*****[] = "*****",
mother****er[] = "************",
mother****ing[] = "*************";
#endif
It works just great, but my question is, checkForProfanity(char * str) is kind of lame with all its if-statements, is there any way to do this better? Like, with enums or something (i've not learned enums yet so I wouldn't know...)