I want to create a method using pointers to insert a substring within a string. I understand how to do this with arrays, but it is unclear to me how this is done using pointers. Any suggestions would be great!
I want to create a method using pointers to insert a substring within a string. I understand how to do this with arrays, but it is unclear to me how this is done using pointers. Any suggestions would be great!
Hello Mate.... Here is one way of doing it
Code:void insert_substring(char *str, int pos, char *substr) { if (pos > strlen(str)) pos = strlen(str); int shift = strlen(substr); for (int i = strlen(str); i >= pos; i--) str[i + shift] = str[i]; for (int i = 0; i < shift; i++) str[pos + i] = substr[i]; }
Last edited by Salem; 10-21-2022 at 11:09 PM. Reason: Removed crayola