Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ String Compare

Name: Herpy McDerperson 2011-12-11 13:59

What the flying fuck is wrong with my C++ string compare?


//Stem word
string stemWord(string word)
{
    const int word_len = word.length();
    string stems[4] = {"ing","ed","er","s"};
    string temp = "";
    bool isStem;

    if (word_len > 4)
    {
        for (int i = 0; i < 4 || isStem == false; i++)
        {
            if (word.compare(word_len - stems[i].length(), stems[i].length(), stems[i]) == 0)
            {
                for (int x = 0; x < diff; x++)
                {
                    temp[x] += word[x];
                }

                word = temp;
            }      
        }
    }

    return word;
}

The word I'm sending to the is "testing," and this method is supposed to remove the stem (eg. "ing") and return the resulting stemmed word.

According to the MSDN's shitty "still, blahblah.." example (http://www.cplusplus.com/reference/string/string/compare/) this should work perfectly fine.

Name: Anonymous 2011-12-11 19:04

>>10
Enjoy your bloat.


char *stemWord(char *word) {
 static const char *stems[4] = { "ing", "ed", "er", "s" };
 static const int stemlens[4] = { 3, 2, 2, 1 };
 int i, j = strlen(word);
 for(i=0;i<4;i++) {
  char *stempos = word + j - stemlens[i];
  if(j>stemlens[i] && !memcmp(stempos,stems[i],stemlens[i])) {
   *stempos = 0;
   break;
  }
 }
 return word;
}


120 bytes. Your code: 752 bytes.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List