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

Pages: 1-4041-8081-

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 16:55 ID:hy8lzauW

doYourOwnHomework [] = []
doYourOwnHomework (x:xs) | x `notElem` "abc" = x:doYourOwnHomework xs
                         | otherwise = doYourOwnHomework xs

Name: Anonymous 2007-05-11 17:03 ID:ze7o20Cd

>>1

@tail_call
def f(i, o=''):
  if not i: return o
  if i[0] in ['a', 'b', 'c']: return f(i[1:], o)
  return f(i[1:], o + i[0])

Name: Anonymous 2007-05-11 17:10 ID:jHJmv6jQ

def oneWordForcedIndentationOfCodeThreadOver(string):
    (oneWordForcedIndentationOfCodeThreadOver(string[1:]) if string[0] in "abc" else string[0]+oneWordForcedIndentationOfCodeThreadOver(string[1:])) if string else string

Name: Anonymous 2007-05-11 17:11 ID:Heaven

>>3
Tail call optimization? In my Python?

Name: Anonymous 2007-05-11 17:27 ID:ze7o20Cd

>>4
I stupidly did ['a', 'b', 'c'] instead of just 'abc', but your function is not tail-recursive.

>>5
It's more possible than you think!

Name: Anonymous 2007-05-11 17:33 ID:jHJmv6jQ

>>4
Whoops, forgot my return.

Name: Anonymous 2007-05-11 17:36 ID:ze7o20Cd

>>7
You could have used lambda

Name: Anonymous 2007-05-11 17:53 ID:9uDEcpvO

C or Java are _so_ the wrong tools for the job. And you're making design choices (e.g. recursion) prematurely. In UNIX ...

echo String | tr -d 'abc'

Name: Anonymous 2007-05-11 17:55 ID:Heaven

>>9
It's clearly a homework assignment.

Name: Anonymous 2007-05-11 18:00 ID:jHJmv6jQ

>>8
But then there'd be no FORCED INDENTATION.
>>10
Then the teachers are goddamn idiots. HAVE FUN OVERFLOWING THE STACK ON MODERATELY LARGE STRINGS.

Name: Anonymous 2007-05-11 18:03 ID:LrEob0br

Since OP wanted something like C or Java...
Here's some pseudocode to get you started. Thought this up in about one minute, it might delete your pron folder or just fail it, although it probably won't.

Also, you may want to make the function take the unwanted characters as arguments.

string removeshit (string s){
  if(we come across one a, b or c in s){
    return removeshit(s with the one offending character removed);
  }
  return s;
}

Name: Anonymous 2007-05-11 18:04 ID:tCgeez6Q

>>11

OP here. It's for a comparative between iterative and recursive timings, memory consuption, etc... Which is all good, if it wasn't for the fact that I did...  one recursive function in my all life (factorial).

Name: Anonymous 2007-05-11 18:05 ID:LrEob0br

>>12 here.
Oh, and just so you won't get the wrong idea, there's better ways to do this than massive recursion. Also cocks.

Name: Anonymous 2007-05-11 18:09 ID:Heaven

>>13
Go read SICP. Now.

Name: Anonymous 2007-05-11 18:10 ID:bLm7QA6k

char *removeCharacters(char *string,char removeMe)
{
int i;
int length;
for(length = 0;string[length]!='\0';length++){}
char *newString = malloc(sizeof(char) * length);

for(length = 0;string[length]!='\0';length++)
{

if(string[length]==removeMe)
{
for
}

}


}

Name: Anonymous 2007-05-11 18:10 ID:bLm7QA6k

woops, I clicked reply I'll finish the code lol

Name: Anonymous 2007-05-11 18:11 ID:ze7o20Cd

>>13
one recursive function in my all life (factorial).
OMG. You need to read SICP. For real, I really recommend it. Just chapter 1, and you'll become an EXPERT PROGRAMMER of recursion. Also,

It's for a comparative between iterative and recursive timings, memory consuption, etc.
Who cares. Ah, you're in your OMG OPTIMIZED phase. Well, I can tell you about it right now:
1. The non-recursive function will be faster on almost all compilers or interpreters.
2. The non-recursive function will take less memory on all compilers or interpreters that don't do tail call/recursion optimization.
3. The above facts are irrelevant compared to development time and costs. Your time > your hardware resources. You can make far more money in the time you save, or just be lazy.

Name: Anonymous 2007-05-11 18:11 ID:TcFyHC4t

2 recursions for the price of one.

contains c [] = False
contains c (char:chars)
  | c == char = True
  | otherwise = contains c chars

remove bad chars = remove' bad chars []
remove' bad [] acc = reverse acc
remove' bad (char:chars) acc
  | contains char bad = remove' bad chars acc
  | otherwise         = remove' bad chars (char:acc)

Name: Anonymous 2007-05-11 18:15 ID:QC7ohZ6D


char *removeCharacters(char *string,char removeMe)
{
int i;
int length;
for(length = 0;string[length]!='\0';length++){}
char *newString = malloc(sizeof(char) * length);

for(length = 0;string[length]!='\0';length++)
{

if(string[length]==removeMe)
{
for
}

}


}

Name: Anonymous 2007-05-11 18:28 ID:Heaven

THE PROPER WAY TO DO IT IN C (I.E., NO PUSSY RECURSION SHIT):

#include <string.h>

void removeChar(char *s, char c) {
    int charShift = 0, i, len = strlen(s)+1;

    for (i = 0; i < len; i++) {
        s[i-charShift] = s[i];
        if (s[i] == c)
            charShift++;
    }
}


NOW KILL YOURSELF, FOR THE GOOD OF PROGRAMMERS EVERYWHERE.

Name: Anonymous 2007-05-11 18:48 ID:bLm7QA6k

#include <stdio.h>

char *removeCharacters(char *string,char removeMe)
{
int i = 0;
int length = 0;
int i2 = 0;
int somethingChanged = 0;
for(length = 0;string[length]!='\0';length++){}
printf("%i\n",length);
char *newString = malloc(sizeof(char) * length);
printf("%s\n",string);
for(i = 0;string[i]!='\0';i++)
{

if(string[i]==removeMe)
{
for(i2=0;i2<length;i2++)
{

if(somethingChanged!=1)
{
if(string[i2]!=removeMe)
newString[i2]=string[i2];
else
{
somethingChanged = 1;
i2++;
}
}
else
{
newString[i2-1]=string[i2];
}

}
break;
}

}
if(somethingChanged==1)
return removeCharacters(newString,removeMe);
else
return string;
}

int main(int argc,char *argv[])
{
char lolWut[] = "AAAABBBAA";
printf("%s\n",lolWut);
printf("%s\n",removeCharacters(lolWut,'A'));
return 0;
}


Not working, I tried.

Name: Anonymous 2007-05-11 19:05 ID:bLm7QA6k

#include <stdio.h>

char *removeCharacters(char *string,char removeMe)
{
int i = 0;
int length = 0;
int i2 = 0;
static long firstTime = 1;
int somethingChanged = 0;
for(length = 0;string[length]!='\0';length++){}
printf("%i\n",length);
char *newString = malloc(sizeof(char) * length-1);
printf("%s\n",string);
for(i = 0;string[i]!='\0';i++)
{

if(string[i]==removeMe)
{
for(i2=0;string[i2]!='\0';i2++)
{

if(somethingChanged!=1)
{
if(string[i2]!=removeMe)
newString[i2]=string[i2];
else
{
somethingChanged = 1;
}
}
else
{
newString[i2-1]=string[i2];
}

}
break;
}

}
if(firstTime)
{
firstTime = 0;
}
else
{
free(string);
}
if(somethingChanged==1)
return removeCharacters(newString,removeMe);
else
return string;
}

int main(int argc,char *argv[])
{
char lolWut[] = "BBAAAABBBAABBB";
printf("%s\n",lolWut);
printf("%s\n",removeCharacters(lolWut,'A'));
return 0;
}

Name: Anonymous 2007-05-11 19:08 ID:bLm7QA6k

Now working :

#include <stdio.h>

char *removeCharacters(char *string,char removeMe)
{
int i = 0;
int length = 0;
int i2 = 0;
static long firstTime = 1;
int somethingChanged = 0;
for(length = 0;string[length]!='\0';length++){}
printf("%i\n",length);
char *newString = malloc(sizeof(char) * length-1);
printf("%s\n",string);
for(i = 0;string[i]!='\0';i++)
{

if(string[i]==removeMe)
{
for(i2=0;i2<length+1;i2++)
{

if(somethingChanged!=1)
{
if(string[i2]!=removeMe)
newString[i2]=string[i2];
else
{
somethingChanged = 1;
}
}
else
{
newString[i2-1]=string[i2];
}

}
break;
}

}
if(firstTime)
{
firstTime = 0;
}
else
{
free(string);
}
if(somethingChanged==1)
return removeCharacters(newString,removeMe);
else
return string;
}

int main(int argc,char *argv[])
{
char lolWut[] = "BBAAAABBBAABBB";
printf("%s\n",lolWut);
printf("%s\n",removeCharacters(lolWut,'A'));
return 0;
}

Name: Anonymous 2007-05-11 19:10 ID:bLm7QA6k

char *removeCharacters(char *string,char removeMe)
{
int i = 0;
int length = 0;
int i2 = 0;
static long firstTime = 1;
int somethingChanged = 0;
for(length = 0;string[length]!='\0';length++){}
char *newString = malloc(sizeof(char) * length-1);
for(i = 0;string[i]!='\0';i++)
{

if(string[i]==removeMe)
{
for(i2=0;i2<length+1;i2++)
{

if(somethingChanged!=1)
{
if(string[i2]!=removeMe)
newString[i2]=string[i2];
else
{
somethingChanged = 1;
}
}
else
{
newString[i2-1]=string[i2];
}

}
break;
}

}
if(firstTime)
{
firstTime = 0;
}
else
{
free(string);
}
if(somethingChanged==1)
return removeCharacters(newString,removeMe);
else
return string;
}


Name: Anonymous 2007-05-11 19:15 ID:LrEob0br

>>25
Uh, what the fuck. That could be coded in less than 10 lines, recursive or not.

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

In Java you can use .substring or something.

Name: Anonymous 2007-05-11 19:16 ID:bLm7QA6k

#include <stdio.h>

char *removeCharacters(char *string,char removeMe)
{
int i = 0;
int length = 0;
int i2 = 0;
static long firstTime = 1;
int somethingChanged = 0;
for(length = 0;string[length]!='\0';length++){}
char *newString = malloc(sizeof(char) * length-1);
for(i = 0;string[i]!='\0';i++)
{

if(string[i]==removeMe)
{
for(i2=0;i2<length+1;i2++)
{

if(somethingChanged!=1)
{
if(string[i2]!=removeMe)
newString[i2]=string[i2];
else
{
somethingChanged = 1;
}
}
else
{
newString[i2-1]=string[i2];
}

}
break;
}

}
if(firstTime)
{
firstTime = 0;
}
else if(somethingChanged)
{
free(string);
}
if(somethingChanged==1)
return removeCharacters(newString,removeMe);
else
return string;
}


int main(int argc,char *argv[])
{
char lolWut[] = "BBAAAABBBAABBB";
printf("%s\n",lolWut);
printf("%s\n",removeCharacters(lolWut,'A'));
printf("%s\n",lolWut);
return 0;
}

Name: Anonymous 2007-05-11 19:18 ID:bLm7QA6k

>>26
<3 losing my time coding endless functions optimizatoring time

Name: Anonymous 2007-05-11 19:19 ID:bLm7QA6k

#include <stdio.h>

char *removeCharacters(char *string,char removeMe)
{
int i = 0;
int length = 0;
static long firstTime = 1;
int somethingChanged = 0;
for(length = 0;string[length]!='\0';length++){}
char *newString = malloc(sizeof(char) * length-1);
for(i=0;i<length+1;i++)
{
if(somethingChanged!=1)
{
if(string[i]!=removeMe)
newString[i]=string[i];
else
{
somethingChanged = 1;
}
}
else
{
newString[i-1]=string[i];
}
}
if(firstTime)
{
firstTime = 0;
}
else if(somethingChanged)
{
free(string);
}
if(somethingChanged==1)
return removeCharacters(newString,removeMe);
else
return string;
}


int main(int argc,char *argv[])
{
char lolWut[] = "BBAAAABBBAA";
printf("%s\n",lolWut);
printf("%s\n",removeCharacters(lolWut,'A'));
printf("%s\n",lolWut);
return 0;
}

Name: Anonymous 2007-05-11 19:26 ID:bLm7QA6k

GETTING SHORTER :

#include <stdio.h>

char *removeCharacters(char *string,char removeMe)
{
int i = 0;
int length = 0;
static long firstTime = 1;
int somethingChanged = 0;
for(length = 0;string[length]!='\0';length++){}
char *newString = malloc(sizeof(char) * length-1);
for(i=0;i<length+1;i++)
{
if(somethingChanged)
newString[i-1]=string[i];
if(string[i]!=removeMe&&!somethingChanged)
newString[i]=string[i];
else
somethingChanged = 1;
}
if(firstTime)
{
firstTime = 0;
}
else if(somethingChanged)
{
free(string);
}
if(somethingChanged)
return removeCharacters(newString,removeMe);
else
return string;
}


int main(int argc,char *argv[])
{
char lolWut[] = "BBAAAABBBAA";
printf("%s\n",lolWut);
printf("%s\n",removeCharacters(lolWut,'A'));
printf("%s\n",lolWut);
return 0;
}

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

>>22
>>23
>>24
>>25
>>28
>>30
This is madness.

Name: Anonymous 2007-05-11 19:39 ID:tCgeez6Q

>>32

THIS IS SPARTA!!!!!!


I'll code something in Java tomorrow. Sleep tight.

Name: Anonymous 2007-05-11 19:56 ID:3yLVP/cT

>>33

Not anymore...

Name: Anonymous 2007-05-11 20:04 ID:LrEob0br

I must say.. all those temporary variables and the use of if-else make this code look like the stuff at The Daily WTF. Think a bit about what the code's doing and how C strings work. You could code an iterative solution by selectively incrementing and copying between two pointers. Not only would the resulting code be very short, it'd also look very hackish. A recursive solution could then be based on that.

It might also run quite fast since the compiler would just use one register for each pointer, and some other for temporary storage. The C source you've posted so far probably compiles into a dozen jump instructions and retarded fumbling with the stack (not enough registers to fit all the variables in). Not good for speed. Since you're coding in C, you might as well know this.

Name: Anonymous 2007-05-11 20:42 ID:Heaven

char * remove_characters( char * str, char chr )
{
    register char * tmp = str;

    while( * str )
    {
        while( * str == chr )
        {
            str++;
        }

        *tmp++ = * str++;
    }

    * tmp = 0;
    return str;
}

Name: Anonymous 2007-05-11 20:55 ID:Heaven

#include <stdio.h>

void remove_char_from_string(char *string,char char_to_remove){
 int i,j=0,l=strlen(string);
 char s2[l];
 for(i=0;i<l;++i)if(!(string[i]==char_to_remove))s2[j++]=string[i];
 s2[j]=0;
 memcpy(string,s2,l);
}

Name: Anonymous 2007-05-11 21:04 ID:Heaven

>>36
>>37
Now that's moar liek it.

Name: Anonymous 2007-05-11 21:57 ID:jHJmv6jQ

void removeChar(char *s, char c) {
    char *to = s, *from = s;
    for (; *from; from++)
        if (*from != c)
            *to++ = *from;
    *to = 0;
}

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

>>39

That was my original code, but I decided to go for the double while loop instead.

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

Name: Anonymous 2007-05-16 8:36 ID:JGbNych/

>>76
I'm surprised that you are so fucking blind. >>2

Name: Anonymous 2007-05-16 12:24 ID:2mrmEDiW

Hint for all you morons using malloc and new: The returned string is NEVER longer than the original.

Name: Anonymous 2007-05-16 13:26 ID:/oxBU8em

Use a general search and replace function replacing "a" with "", "b" with "" etc.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char * str_search_and_replace(char * input, char * search, char * replace) {
    int input_length, search_length, replace_length;
    int search_index;
//    int number_of_replacements;
    int output_length;
    char * output;
   
    input_length = strlen(input);
    search_length = strlen(search);
    replace_length = strlen(replace);
    search_index = 0;
//    number_of_replacements = 0;
    output_length = 1;
    output = malloc(1);
   
    while(search_index < input_length-search_length) {
        if(strncmp(input + search_index, search, search_length) == 0) {
            output = realloc(output, output_length+replace_length);
           
            strncpy(output + output_length-1, replace, replace_length);
           
            search_index += search_length;
            output_length += replace_length;
           
//            number_of_replacements++;
        }
        else {
            output = realloc(output, output_length+1);
           
            output[output_length-1] = input[search_index++];
            output[output_length] = '\0';
           
            output_length++;
        }
    }
   
    output = realloc(output, output_length + input_length-search_index);
    strncpy(output + output_length-1, input + search_index, input_length-search_index);
   
    return output;
}

Name: Anonymous 2007-05-16 19:37 ID:/LycOJun

>>83
You do NOT need to use memory functions!

Here is another hint: Return an unsigned int instead of a char *, but first you have to write the right function.

Name: Anonymous 2007-05-17 16:43 ID:+Ug3t86q

This is the correct way to do it iteratively in C:

//Null terminated strings 'str' and 'rem'
char *strrem(char *str, const char *rem) {
    char *ip, *sp, *rp;
   
    if (str && *str && rem && *rem) {
        for (ip = sp = str; *sp; ++sp) {
            for (rp = (char *) rem; *rp; ++rp) {
                if (*sp == *rp) {
                    break;
                }
            }
           
            if (!*rp) {
                *ip++ = *sp;
            }
        }
       
        while (*ip) {
            *ip++ = '\0';
        }
     }   
    
     return str;
}

I'll leave it to you to find the recursive version which isn't terribly difficult to find.

Name: Anonymous 2007-05-17 16:52 ID:+Ug3t86q

Indentation fixed for >>85 :

//Null terminated strings 'str' and 'rem'
char *strrem(char *str, const char *rem) {
    char *ip, *sp, *rp;
 
    if (str && *str && rem && *rem) {
        for (ip = sp = str; *sp; ++sp) {
            for (rp = (char *) rem; *rp; ++rp) {
                if (*sp == *rp) {
                    break;
                }
            }
           
            if (!*rp) {
                *ip++ = *sp;
            }
        }
       
        while (*ip) {
            *ip++ = '\0';
        }
    }
   
    return str;
}

Name: Anonymous 2007-05-19 17:50 ID:twjc7PJ9

*Bump*

Copy this so you never write a bad char removal function again.

Name: Anonymous 2007-05-19 19:38 ID:nrq1NcT2

Over 9,000! (bugs)

Name: Anonymous 2009-01-14 14:50

Why reinvent the wheel?

Name: Anonymous 2009-02-25 7:57

The SDL code gets   included in your   ass key art.

Name: Sgt.Kabu췙䐆kiman嫍ᐛ 2012-05-29 1:37

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

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