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

Pages: 1-

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 13:59

Oops forgot the code tags, fuck you that's why.

Name: Anonymous 2011-12-11 14:09

What the flying fuck is this OOP shit doing on my /prog/?

Name: Anonymous 2011-12-11 14:19

God damn sepples, when will you people learn?

Name: Anonymous 2011-12-11 14:39

>>3
Well shit, SOMEONE has to do their part to cull all the garbage/irrelevant programming language bullshit on here

>>4
Wat.

Name: Anonymous 2011-12-11 14:56

uninitialised isStem in loop condition, not possible to figure out wtf is this sepples about

Name: Anonymous 2011-12-11 15:03

>>6
Derp, thanks.

Unfortunately, that isn't the problem, as I've been getting the same fail prior to entering that shit.

Name: Anonymous 2011-12-11 15:05

string subscript out of range

Name: Anonymous 2011-12-11 15:11


  for (int x = 0; x < diff; x++)
                {
                    temp[x] += word[x];
                }

                word = temp;

OP iseither a total noob or is trolling us with incomplete code or something, where tf is 'diff' and temp is initialised to empty string so temp[x] is prolly just getting some random memory :o

Name: Anonymous 2011-12-11 15:12

>>9

My bad, I posted some mixed/matched code. This should be correct:


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

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

                word = temp;
            }       
        }
    }

    return word;
}

Name: Anonymous 2011-12-11 15:15

>>9

>temp is initialised to empty string so temp[x] is prolly just getting some random memory :o

Derp, I'm retarded, that was it. It should be:
temp[x] += word[x]   ->   temp += word[x]

Thanks

Name: Anonymous 2011-12-11 15:15


string anusify(string faggot)
{
    const int anus_count = faggot.length();
    string anus = faggot;
    int finish = anus_count;
   
    if(anus_count > 4){
        if(faggot[anus_count-1] == 's'){
            finish = anus_count-1;
        } else if( (faggot[anus_count-1] == "r" && faggot[anus_count-2] == "e" ) ||
                   (faggot[anus_count-1] == "d" && faggot[anus_count-2] == "e" )){
            finish = anus_count-2;
        } else if( (faggot[anus_count-1] == "g" && faggot[anus_count-2] == "n" && faggot[anus_count-3] == "i"){
            finish = anus_count-3;
        }
        if(anus_count != faggot.length){
            anus = "";
            for(int i = 0;i<anus_count;++i){
                anus[i] = faggot[i];
            }
        }
    }
    return anus;
}

Name: Anonymous 2011-12-11 15:21

>>12
Your identation style is shit.

Name: Anonymous 2011-12-11 15:26

>>13
I could use 2 spaces for indenting if you want me to but i was to lazy to open up emacs

Name: Anonymous 2011-12-11 15:26

>>12
>>13
I agree, it looks like a goddamn spaceship. Also, it hurts my brain to figure out what the fuckshitballs is going on in there.

Name: Anonymous 2011-12-11 15:26

>>14
not to mention it matches OP, so suck my dick faggot

Name: Anonymous 2011-12-11 15:28

>>15
If you're having trouble figuring out what that's doing then you probably shouldn't be attempting this problem at all

and there's a slight type:

if(anus_count != faggot.length){
for(int i = 0;i<anus_count;++i)

should be:

if(finish != faggot.length()){
for(int i = 0;i<finish;++i)

Name: Anonymous 2011-12-11 15:30

>>16
OP forgot to use code tags, see >>10

Name: Anonymous 2011-12-11 15:31

>>18
that's still a 4 space indent.

Name: Anonymous 2011-12-11 15:39

NIGGERS

Name: Anonymous 2011-12-11 17:17

>>19

Yes but your code looks sloppy as fuck. You should be doing this:

string anusify(string faggot)
{
    const int anus_count = faggot.length();
    string anus = faggot;
    int finish = anus_count;
  
    if(anus_count > 4)
    {
        if(faggot[anus_count-1] == 's')
            finish = anus_count-1;
    else if( (faggot[anus_count-1] == "r" && faggot[anus_count-2] == "e" ) || (faggot[anus_count-1] == "d" && faggot[anus_count-2] == "e" ))
        finish = anus_count-2;
    else if( (faggot[anus_count-1] == "g" && faggot[anus_count-2] == "n" && faggot[anus_count-3] == "i")
        finish = anus_count-3;
        if(anus_count != faggot.length)
        {
            anus = "";
            for(int i = 0;i<anus_count;++i)
                anus[i] = faggot[i];
        }
    }
    return anus;
}

Name: Anonymous 2011-12-11 17:18

^ Welp even I fucked up the intending, but you get the idea: you're a fat nigger

Name: Anonymous 2011-12-11 18:17

>>22
you removed the braces, good one.


Style of choice is a subjective topic, you can go back to being a faggot now

Name: Anonymous 2011-12-11 18:28

>>21
1. you have unmatched brace right before line "return anus"
2. you have a unmatched ( at the else if before "finish = anus_count-3"
3. you waste a whole line for starting brace and try to call someone elses style bad?
4. both of you checked "e" twice when it didn't need to be in the second else if


string anusify(string faggot){
    const int anus_count = faggot.length();
    string anus = faggot;
    int finish = anus_count;
 
    if(anus_count > 4){
        if(faggot[anus_count-1] == 's')
            finish = anus_count-1;
        else if( (faggot[anus_count-2] == "e") && (faggot[anus_count-1] == "r" || faggot[anus_count-1] == "d") )
            finish = anus_count-2;
        else if( (faggot[anus_count-1] == "g" && faggot[anus_count-2] == "n" && faggot[anus_count-3] == "i") )
            finish = anus_count-3;
        if(anus_count != faggot.length){
            anus = "";
            for(int i = 0;i<anus_count;++i)
                anus[i] = faggot[i];
        }
        return anus;
    }
}

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.

Name: Anonymous 2011-12-11 19:09

>>25
dat memcmp,
dat cool C style solution you will never be able to use anymore with modern bloat
I cried inside for memcmp & memcpy

Name: Anonymous 2011-12-11 19:28

>>26
The funny thing is, the C++ code compiles into something that calls strlen() and something named ?compare@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEHIIABV12@II@Z in the lib that eventually calls memcmp().

Name: Anonymous 2011-12-11 19:35

>>27
welcome to a language that compiles to C then to asm

Name: Anonymous 2011-12-11 20:57

>>28
At least strlen and memcmp in C look pretty similar to what they would be in asm.

Name: Anonymous 2011-12-11 22:31


std::string a = "op does not know how to program";
std::string b = "that's ok, neither does /prog/";
if (a != b) std::cout << "fucking miracles";

Name: Anonymous 2011-12-11 22:32

>>29
No they wouldn't you moron jew. And the fact that you think they are shows that you really do work some shit hourly job.

Name: Anonymous 2011-12-11 22:42

>>31
He obviously meant the exported function names created by C++mongler.

Name: Anonymous 2011-12-11 23:36

>>31
Cool story bro.

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