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

Recursive Character Search and Destroy

Name: Anonymous 2007-05-11 16:50 ID:tCgeez6Q

I receive a string, and I want to remove any 'a', 'b' and 'c' characters, in a recursive way.

How do I do this in C and Java?

Thanks

Name: Anonymous 2007-05-11 22:26 ID:Heaven

>>40

Come to think of it, I don't even know why I bothered doing that though. Except for variation.

Name: Anonymous 2007-05-11 22:49 ID:Heaven

>>39
void remove_char(char* s,char c){
 for(char*r=s;*s;s++)if(*s!=c)*r++=*s;
}

Name: Anonymous 2007-05-11 22:58 ID:Heaven

>>42
oops...
void remove_char(char*s,char c){
 for(char*r=s;*s;*r=!s++)if(*s!=c)(*r++=*s);
}

Name: Anonymous 2007-05-12 4:17 ID:Heaven

>>16
>>20
>>21
>>22
>>23
>>24
>>25
>>28
>>30
>>31
>>36
>>37
>>39
What part of ``Recursive Character Search and Destroy'' you fail to understand? Or does this mean that none of you sandcore C programmers understand the concept of recursion well enough to write anything except perhaps a factorial function?

Name: Anonymous 2007-05-12 4:20 ID:Heaven

>>44

Oops, I should troll a bit more carefully, I forgot

>>42
>>43

from my list.

Name: Anonymous 2007-05-12 5:25 ID:Lxchqixg

If C isn't sandcore enough, here's some brainfuck.
,+[->+>++++++++[<++++++++++++>-]>[-]>[-]>[-]
<<<<[>+>+<<-]<[>+>>>+>+<<<<<-]>[>-<-]>[<<+>>[-]]+>+[<+<+>>-]
>[<+>>>+<<-]>>[<<+>>-]<<<[<<->>-]<<[<+>[-]]>[>>-<<-]
>>[<<<<+>>>>[-]]<<<<[-[-[>>>>>.<<<<<[-]][-]][-]],+]

Name: Anonymous 2007-05-12 5:27 ID:Heaven

(no, it's not recursive, but recursive programming isn't touring complete anyway)

Name: Anonymous 2007-05-12 11:56 ID:fmdmxFJk

My recursive loop stopped after 13256278887989457651018865901401704640 iterations.

Name: Anonymous 2007-05-12 12:12 ID:5QjmMKeZ

>>3
>>4
ONE WORD, THE FORCED INDENTATION OF CODE, THREAD OVER

>>18
Is that even relevant or just your way of saying, "ive read SICP!"?!

>>46
lol thats so leet

Name: Anonymous 2007-05-12 13:25 ID:XdDrpvWG

void remove_characters( char * str, char chr )
{
    size_t right_length;

    if ( !* str )
    {
        return;
    }

    if ( * str == chr )
    {
        if ( * ( str + 1 ) )
        {
            right_length = strlen( str );
            memmove( str, str + 1, right_length );
        }
        else
        {
            * str = 0;
            return;
        }
    }
    else
    {
        str++;
    }

    remove_characters( str, chr );
}

void remove_characters2( char * str, char * bstr )
{
    size_t right_length;
    register char * ptr;

    if ( !* str )
    {
        return;
    }

    for( ptr = bstr; * ptr; ptr++ )
    {
        if ( * str == * ptr )
        {
            right_length = strlen( str );
            memmove( str, str + 1, right_length );
            ptr = NULL;
            break;
        }
    }

    if ( ptr )
    {
        str++;
    }

    remove_characters2( str, bstr );
}

Name: Anonymous 2007-05-12 13:27 ID:llSAt5l6

>>50
   if ( !* str )

Fail.

Name: Anonymous 2007-05-12 13:29 ID:Heaven

>>51

wut

Name: Anonymous 2007-05-12 13:30 ID:llSAt5l6

>>52
One word, the fallacy of retarded spacing.  Thread over.

Name: Anonymous 2007-05-12 13:31 ID:Heaven

>>53

<opinion>

Name: Anonymous 2007-05-12 13:33 ID:Heaven

>>54
lol man at first I was like what is that he's doing but then I saw it and I was like lol all the time because it's so funny when you do that and it always makes me smile because I know these things I try to do them myself but I'm not good at them or anything so like I'd rather watch other people do them because then it's just as funny even though I can't do it myself or anything like that lol

Name: Anonymous 2007-05-12 13:34 ID:Heaven

>>55

The funny thing is, I wrote that.

Name: Anonymous 2007-05-12 13:45 ID:llSAt5l6

>>56
lol man at first I was like what is that he's doing but then I saw it and I was like lol all the time because it's so funny when you do that and it always makes me smile because I know these things I try to do them myself but I'm not good at them or anything so like I'd rather watch other people do them because then it's just as funny even though I can't do it myself or anything like that lol

Name: Anonymous 2007-05-12 13:49 ID:DUbtzT6C

>>57

lol man at first I was like what is that he's doing but then I saw it and I was like lol all the time because it's so funny when you do that and it always makes me smile because I know these things I try to do them myself but I'm not good at them or anything so like I'd rather watch other people do them because then it's just as funny even though I can't do it myself or anything like that lol

Name: Anonymous 2007-05-12 14:08 ID:pM7XIwIm

>>56
That's not funny at all.

Name: Anonymous 2007-05-12 14:12 ID:Heaven

>>59

Figure of speech

Name: Anonymous 2007-05-12 14:29 ID:Heaven

>>60
Here in /prog/, we roll our own.

Name: Anonymous 2007-05-12 14:40 ID:Heaven

>>61
lol man at first I was like what is that he's doing but then I saw it and I was like lol all the time because it's so funny when you do that and it always makes me smile because I know these things I try to do them myself but I'm not good at them or anything so like I'd rather watch other people do them because then it's just as funny even though I can't do it myself or anything like that lol

Name: Anonymous 2007-05-12 20:06 ID:+FERuxZR

>>44
void remove_char(char* s,char c){
 for(char*r=s;*s;*r=!s++)if(*s!=c)*r++=*s;
}


void remove_char(char*s,char c,char*r){
 if(!r)r=s;
 if(*s!=c)*r++=*s;
 *r=!s++;
 if(*s)return remove_char(s,c,r);
}


yeah, recursion makes it so much better.

Name: Anonymous 2007-05-12 20:14 ID:1BaoTvIh

>>63
Recursive character removal is ENTERPRISE QUALITY.

Name: Anonymous 2007-05-12 20:23 ID:Heaven

>>63
s/return //

Name: Anonymous 2007-05-12 20:44 ID:fmdmxFJk

>>64
No. Enterprise would never do anything smart, like recursion.

Name: Anonymous 2007-05-12 20:51 ID:1BaoTvIh

>>66
Recursion in general isn't ENTERPRISE QUALITY. However, using recursion to do character removal with O(n^2) space usage is definitely ENTERPRISE QUALITY.

Name: Anonymous 2007-05-15 3:17 ID:vE14gqt1

Recursion involves calling the same function more than once.

WAKEUP PEOPLE!

Name: Anonymous 2007-05-15 4:23 ID:APoogirR

>>68
Dumbass, recursion involves calling the same function FROM ITSELF. It doesn't matter if you call it one time or seven million, as long as the same function is listed in two descendant frames in the backtrace.

Name: Anonymous 2007-05-15 5:05 ID:vE14gqt1

>>69
IT DOES involve calling the same function more than once. I never said otherwise.

void dumbass(unsigned int i) {}
void youare(unsigned int i) {
 while (--i) {
  thedumbass(i);
 }
}

Name: Anonymous 2007-05-15 5:15 ID:vE14gqt1

Correction:

void youarethedumbass(unsigned int i) {
    while (--i) {
        youarethedumbass(i);
    }
}

Forgive me, I am badly drunk.

Name: Anonymous 2007-05-15 18:36 ID:Heaven

>>71
Holy shit, that function called itself.
Keep talking, your idiocy amuses me.

Name: Anonymous 2007-05-15 18:57 ID:BzTHZt/W

in java.

    public static String tehMassRemoval(char a, char b, char c, String s)
    {
        for(int i = 0; i<s.length(); i++)
        {
            if (s.charAt(i) == a || s.charAt(i) == b || s.charAt(i) == c)
            {
                s = tehMassRemoval(a,b,c,(s.substring(0, i) + s.substring(i+1)));
            }

        }
        return s;
    }

Name: Anonymous 2007-05-16 3:17 ID:Qn1G05ZA

if (s.charAt(i) == a || s.charAt(i) == b || s.charAt(i) == c)
ew

Name: Anonymous 2007-05-16 4:36 ID:2mrmEDiW

>>73
Java sucks.

Name: Anonymous 2007-05-16 5:16 ID:ytzgLpDV

I'm surprised noone has done a Haskell version yet. Oh well, here you go:
remove a b c = filter (/= a) . filter (/= b) . filter (/= c)
It would of course be trivial to have it take a list of items to remove instead of just 3. Also note that thanks to Haskell's generic-by-default-ness this works on lists of any type, not just strings.

Haskell wins once again.

Name: Anonymous 2007-05-16 5:23 ID:SdbwevHE

s/[abc]//g

Name: Anonymous 2007-05-16 5:24 ID:Heaven

>>73
Not enough factories.

Name: Anonymous 2007-05-16 5:47 ID:fIQ22TUZ

>>76
LOL fail moar.

remove = filter (not.(`elem` "abc"))

Name: Anonymous 2007-05-16 6:06 ID:ytzgLpDV

>>79
no u

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