It is written in haste, so you will most likely have to test it ( at least here ).
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char *str = "Can you please tell me how to change this ringtone ?", *wrd = "No"; size_t required_inserts = 0; char* beg = str, *lst = beg; while (beg < strlen(str) + str) { if (*beg == ' ') ++required_inserts; ++beg; } required_inserts /= 3; size_t str_sz = strlen(str) + required_inserts * (strlen(wrd) - 1) // additional space + 1; // termination symbol char *new_str = (char*)malloc(str_sz * sizeof(*new_str)); beg = str; size_t counter = 0, bytes_written = 0; while (1) { if ((beg = strpbrk(beg + 1, " ")) == 0) break; ++counter; if (counter % 3 == 0) { memcpy(bytes_written + new_str, lst, beg - lst); bytes_written += (beg - lst); lst = beg + 1; memcpy(bytes_written + new_str, wrd, strlen(wrd)); bytes_written += strlen(wrd); } } if (lst) { memcpy(bytes_written + new_str, lst, str + strlen(str) - lst); } printf("out: %s\n", new_str); free(new_str); }
"нет"(if the prep starts to say something, answer that you are not a masochist) - avpstrchr, put all the characters before it, then"нет", then look for the space again. - HolyBlackCat