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 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.

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