Problem with char returning strange characters

1

The intention is to replace all words toda with 0 , however after X characters it begins to return strange values as can be observed in the comments.

I think I'm handling the parameters wrong, but at what point? Why?

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

/*
 * @brief
 * PHP's str_replace ported to C
 * @author Silver Moon ([email protected])
 * 
 */

char *str_replace(char *search , char *replace , char *subject)
{
    char  *p = NULL , *old = NULL , *new_subject = NULL ;
    int c = 0 , search_size;

    search_size = strlen(search);

    //Count how many occurences
    for(p = strstr(subject , search) ; p != NULL ; p = strstr(p + search_size , search))
    {
        c++;
    }

    //Final size
    c = ( strlen(replace) - search_size )*c + strlen(subject);

    //New subject with new size
    new_subject = malloc( c );

    //Set it to blank
    strcpy(new_subject , "");

    //The start position
    old = subject;

    for(p = strstr(subject , search) ; p != NULL ; p = strstr(p + search_size , search))
    {
        //move ahead and copy some text from original subject , from a certain position
        strncpy(new_subject + strlen(new_subject) , old , p - old);

        //move ahead and copy the replacement text
        strcpy(new_subject + strlen(new_subject) , replace);

        //The new start position after this search match
        old = p + search_size;
    }

    //Copy the part after the last search match
    strcpy(new_subject + strlen(new_subject) , old);

    return new_subject;
}


char* change(char *original) {
    char end[100];

    strcpy(end, str_replace("toda",        "0", original));

    return end;
}

int main()
{
    char *original[100],
         *final[100];

    printf("digite:");

    fgets(original, sizeof(original), stdin); // obtem valor -> toda toda toda toda toda toda toda toda

    strcpy(final, change(original)); // salva em final o valor alterado
    printf("[%s]", final); // retorna: [0 670 0 0 0 0 007_7]

    return 0;
}
    
asked by anonymous 03.06.2017 / 18:06

1 answer

1

You did not have to use pointer there in the declaration of the initial variables. If it were to use the pointer it should not use array , it would allocate with malloc() that by signal is used of replace function and gave error for not having its include .

In function change() you should use malloc() to allocate in heap . See the links below for a better understanding. But in fact this function is not necessary, it can be simplified. It might even be useful to abstract the operation, but I do not see a gain in that case. I think even the final variable is unnecessary, but I have not seen if there is any side effect on str_replace() that creates some complicator.

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

char *str_replace(char *search , char *replace , char *subject) {
    char  *p = NULL , *old = NULL , *new_subject = NULL ;
    int c = 0 , search_size;
    search_size = strlen(search);
    //Count how many occurences
    for (p = strstr(subject , search) ; p != NULL ; p = strstr(p + search_size , search)) {
        c++;
    }
    //Final size
    c = ( strlen(replace) - search_size )*c + strlen(subject);
    //New subject with new size
    new_subject = malloc( c );
    //Set it to blank
    strcpy(new_subject , "");
    //The start position
    old = subject;
    for (p = strstr(subject , search) ; p != NULL ; p = strstr(p + search_size , search)) {
        //move ahead and copy some text from original subject , from a certain position
        strncpy(new_subject + strlen(new_subject) , old , p - old);
        //move ahead and copy the replacement text
        strcpy(new_subject + strlen(new_subject) , replace);
        //The new start position after this search match
        old = p + search_size;
    }
    //Copy the part after the last search match
    strcpy(new_subject + strlen(new_subject) , old);
    return new_subject;
}

int main() {
    char original[100], final[100];
    printf("digite:");
    fgets(original, sizeof(original), stdin);
    strcpy(final, str_replace("toda", "0", original));
    printf("[%s]", final);
}

See running on ideone . And at Coding Ground . Also put it on GitHub for future reference .

If you're going to even use change() , which is a waste:

char* change(char *original) {
    char *end = malloc(100);
    strcpy(end, str_replace("toda", "0", original));
    return end;
}

Questions related to more details:

03.06.2017 / 19:00