Recursive Character Search and Destroy
1
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
81
Name:
Anonymous
2007-05-16 8:36
ID:JGbNych/
>>76
I'm surprised that you are so fucking blind.
>>2
82
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.
83
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;
}
84
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.
85
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.
86
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;
}
87
Name:
Anonymous
2007-05-19 17:50
ID:twjc7PJ9
*Bump*
Copy this so you never write a bad char removal function again.
88
Name:
Anonymous
2007-05-19 19:38
ID:nrq1NcT2
Over 9,000! (bugs)
89
Name:
Anonymous
2009-01-14 14:50
Why reinvent the wheel?
90
Name:
Anonymous
2009-02-25 7:57
The SDL code gets included in your ass key art.
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
Newer Posts