-3
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]) {     

char * str = "Testing replace text...\n\n";


        char* buffer = malloc(sizeof(char));
        char* insertPoint = &buffer[0];
        char* copy = str; 
        char* p = strstr(str, "epl");
        char* g = "gard";
        int size = 0; 

        size = p-copy; //p = 9, which is the number of elemts till the first element of the substring
                       //want to allocate this space, and then increment insertPoint, by that amt(it'll be pointing
                       // to nothing)
        buffer = realloc(buffer, size);
        printf("Size: %d\n", size);
        memcpy(insertPoint, copy, size);
        printf("COPY: %s\n", buffer);

        copy += size;
        buffer = realloc(buffer, size+strlen(g));
        insertPoint += size;
        printf("%c", *insertPoint);
        memcpy(insertPoint, g, strlen(g)); //insert after the 9 letters, the string the size of g
        size += strlen(g); //size if the size of the buffer
        printf("Size2: %d\n", size);
        printf("COPY2: %s\n", buffer);

  return EXIT_SUCCESS;
}

Just some quick experimental code; I am just trying to replace the substring epl in str with "gard" but when I print it out there are no changes to the string buffer I am printing, meaning the first string im printing works where it gets all the letters into buffer before the substring occurs, but when I try to replace with substring it doesn't work. I've testing the individual pointers and they all seem correct...not sure what is happening, any insight? Thanks...fully runnable program.

GCGSAUCE
  • 13
  • 3

2 Answers2

0

You are not appending remaining text "ace text..." after replacing "epl".

Can you try code below

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

int main() {

    char *source_string = "Testing replace text...";
    char *search_string = "epl";
    char *replace_string = "gard";

    int search_length = strlen(search_string);
    int replace_length = strlen(replace_string);

    // Find start position of search string
    char *start = strstr(source_string, search_string); // start pointing to "eplace text..."
    int intial_length = start - source_string; // intial_length = 9

    // Get remaining text which should append after replace
    char *remaining_string = (start + search_length); // remaining_string pointing to "ace text..."
    int remaining_length = strlen(remaining_string); // remaining_length = 11

    // Find total length of string after replacing text
    int total_string_length = intial_length + replace_length + remaining_length; // 24

    char *buffer = (char *)malloc(total_string_length + 1); // +1 for null pointer
    char *current_index = buffer;

    // Add initial text
    memcpy(current_index, source_string, intial_length);
    current_index += intial_length;

    // Add replace text
    memcpy(current_index, replace_string, replace_length);
    current_index += replace_length;

    // Add remaining text
    memcpy(current_index, remaining_string, remaining_length);
    current_index += remaining_length;

    memcpy(current_index, "\0", 1); // add null pointer at last
    printf("Final Output: %s", buffer); // Final Output: Testing rgardace text...

    return 0;
}
Lalmani Dewangan
  • 306
  • 2
  • 11
0

I think the problem in your code arise because strlen does not include the terminating zero. I tried to fix your code but in the end I found it easier to re-write it anew (and using more sensible variable names).

The following simple four steps work. The continuous use of strlen may be replaced by variables, but I left them for clarity. (Also, a good compiler may very well optimize this code by leaving the calls out.)

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

int main(int argc, char *argv[])
{     
    char *str = "Testing replace text...\n\n";
    char* buffer;
    char* find_str = "epl";
    char* repl_str = "gard";

    char *find_str_pos = strstr (str, find_str);

    /* 1. create new buffer of the correct size */
    buffer = malloc (strlen(str) - strlen(find_str) + strlen(repl_str) + 1);
    /* 2. copy first part */
    memcpy (buffer, str, find_str_pos - str);
    /* 3. add new text */
    memcpy (buffer + (find_str_pos - str), repl_str, strlen(repl_str));
    /* 4. append original text */
    memcpy (buffer + (find_str_pos - str) + strlen(repl_str), find_str_pos + strlen(find_str), strlen(find_str_pos) - strlen(repl_str) + 1);

    printf ("-> [%s]\n", buffer);

    return EXIT_SUCCESS;

}
Jongware
  • 22,200
  • 8
  • 54
  • 100