There is such a task:
"In the line, instead of spaces, insert a comma and a space."
How to solve it? I think using the lines. But he changed his mind. Put on the right path.
There is such a task:
"In the line, instead of spaces, insert a comma and a space."
How to solve it? I think using the lines. But he changed his mind. Put on the right path.
Yes, as already written, in C there is no standard function for replacing substrings. But it is easy to write it yourself. Here, for example, my govnokod:
char* replace_substring(const char *source, const char *substring, const char *replace_with) { size_t substring_s = strlen(substring); size_t replace_with_s = strlen(replace_with); size_t source_s = strlen(source); char *result = malloc(source_s + 1); char *ptr = result; if(!ptr) return 0; while(*source) { /* Копируем исходную строку до тех пор, пока не встретим нужную подстроку */ if (strncmp(source, substring, substring_s)) { *ptr++ = *source++; } else { ptr -= (int)result; /* Меняем размер новой строки (так как $substring и $replace_with - не всегда будут одиноковы по длине) */ result = realloc(result, source_s += (replace_with_s - substring_s)); /* Копируем $replace_with в новую строку */ ptr += strlen(strcpy(ptr = result + (int)ptr, replace_with)); /* Следующий проход цикла начнем уже со следующего за найденной подстрокой символа */ source += substring_s; } } *ptr = 0; return result; }
Source: https://ru.stackoverflow.com/questions/39607/
All Articles