What is the problem with level 0 of the c language coding test?

Asked 1 years ago, Updated 1 years ago, 324 views

I want to find out one by one while solving the programmer's coding test problem by taking a log point in the visual studio, but I tried solving the programmer's problem without the main door using the main door, where is the problem? This is a specific character replacement problem! There is no problem with the solution function by matching the problem!

char* solution(const char* my_string, const char* letter);

int main(void) {

char str[8] = "hello";
char letter = 'h';

solution(str, letter);
printf(str);

return 0;

}

char* solution(const char* my_string, const char* letter) {

int len = strlen(my_string);
int j = 0;
char* answer = (char*)malloc(len - 1);

for (int i = 0; i < len; i++)
{
    if (my_string[i] != letter[0])
    {
        answer[j] = my_string[i];
        j++;
    }
}
answer[j] = '\0';

return answer;

}

c

2023-02-02 19:35

1 Answers

The solution function is not changing the value of str. Naturally, if str is printf, it will remain the same as the "hello" that was initially set.

In order to get the result you want, I think you need to print the value returned by solution. Try it the way below.

char* answer = solution(str, letter);
printf(answer);


2023-02-03 02:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.