Problem with char returning strange characters

Asked

Viewed 717 times

1

The intention is to replace all words toda for 0, however after X characters it starts returning 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 \336\3770 0 0 0 0 \220\360\277_\377]

    return 0;
}
  • Why do you want to have 100 different strings? And why aren’t they being allocated?

  • @bigown am student, do not know much what I did, put [100] for my debugger stop giving Warning when he tries to run the fgets and strcpy

  • That’s not how it works, every time you find a problem you have to understand what it is and how to solve it. Starting to look for random solutions only increases the problem. What should appear? Eight 0s? You really need this function change()? It seems to me that she is cheerful there. I am even making an answer and I took it. My internet is falling all the time, and I will have to interact less and complement when possible.

  • @bigown at the end of the code realized I got it wrong, all wrong :/. The intention is to appear 8x0 yes. You can explain my mistakes?

1 answer

1


There was no reason to use pointer there in the declaration of the initial variables. If you were to use pointer you should not use array, then would allocate with malloc() which by the way is used of the function of replace and was wrong for not having his include.

In function change() should wear a malloc() to allocate into heap. See the links below to understand better. But in fact this function is not even necessary, It can to simplify. It might even be useful to abstract the operation, but I see no gain in this case. I think until the variable final is unnecessary, but I haven’t seen if it has any side effects on str_replace() that creates some complication.

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

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

If you’re really gonna use change(), which is a waste:

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

Related questions with more details:

  • Thank you very much, I am reading everything you related. However I continue with the problem, the existence of the function change is really unnecessary, but requiring the command of my work.. and apparently the problem is with it, because the error persisted..

  • I showed that it is working. If you have error is something else.

  • you deleted the function change so you don’t see that the error persists, it needs to exist. As I can by malloc in order for it to work? The related links do not demonstrate this.

  • @Elaine put it as it should be. But reinforcement, this is not only unnecessary, it’s a mistake, even if it works.

  • I guess I missed you, that function change serves to change not only a word but a hundred of them, and do the reverse as well.. I did not put this in the question because it would be too much information, and the problem is only in the parameters (I think).. I will search to find how to solve this problem. Thank you

  • What is in the question is answered. And even with what you spoke, it is a mistake to do it this way.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.