Painful censorship

I just wrote this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int find_and_replace(string search, string replace, string &phrase){
  int num_of_replaces = 0;
  for (int temp = 0; temp < phrase.size(); temp++) {
         if (tolower(phrase[temp]) == search[0]) {
            bool found = true;
            for (int temp2 = temp + 1, stemp = 1; temp2 < phrase.size() && stemp < search.size(); stemp++, temp2++) {
               if (tolower(phrase[temp2]) != search[stemp])
                  found = false;
            }
            if (found) {
               for (int temp2 = temp, stemp = 0; temp2 < temp + search.size(); stemp++, temp2++) {
                  phrase[temp2] = replace[stemp % replace.size()];
               }
               temp+= search.size() - 1;
               num_of_replaces++;
            }
         }
      }
  return num_of_replaces;
}


Then stopped and realized... this is horrible.

Haha I'm working on my censorship and testing out the speed of several methods. So far I haven't made a useful algorithm. But has anyone had any censoring experience? Care to share some experience?
Use regular expressions.
I've already looked into them, I will possibly depending on speed. I'm sure it's faster than anything I could hack together.
There are many string searching algorithms available -- you might consider looking at some of them.

As for replacing text, actually a rope might be a better option than std::string, particularly if phrase is large.

A rope eh, looks like I'm off to do some research =)

This is actually going to be implemented on individual devices so my server doesn't have to handle censorship and users can have personalized blacklists so I'm not really worried a ton about size, and I'm unfortunately going to have to port everything to objective-c...

Liking the ropes from what I've read so far =)
Topic archived. No new replies allowed.