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-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;
}

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