For a start, it is desirable to write the correct English words as identifiers. I would replace bufer with buffer
char buffer[250]; ^^^^^^
If you use standard C functions in your program, such as, for example, strlen , then you should include the header <cstring>
#include <cstring>
In fact, when you deal with strings, you can usually do without this function and focus on the final zero line. Otherwise, the call to strlen in sequential processing of the string, as is the case in your program, only takes time.
In this cycle
for(int g = 0; i <= d1; i++) bufer[p] = name[g];
you are trying to copy in the bufer in addition to other characters, also the trailing 0, which is not really necessary.
And most importantly, you do not increase the indices p and g , so copying does not occur, however the index i , which should not increase, increases.
It would be correct to write
for(int g = 0; g < d1; g++) bufer[p++] = name[g];
and the next sentence after the cycle
p = p + d1;
remove.
In this block of code, you must increment the index p (by the way, this is a bad name for the index) after the assignment clause, not before it.
else { p++; bufer[p] = S[i]; }
You could do everything with standard functions such as strchr , memcpy and strcpy , but if you want to practice in cycles, the main program code might look like this
size_t i = 0; for (size_t j = 0; S[j] != '\0'; j++ ) { if (S[j] == '$') { for (size_t k = 0; name[k] != '\0'; k++) { buffer[i++] = name[k]; } } else { buffer[i++] = S[j]; } } buffer[i] = '\0';
Here is a demo program.
#include <iostream> int main() { char S[100] = "Hello $. What are you $ going to say?"; char name[20] = "John Shepard"; char buffer[250]; size_t i = 0; for (size_t j = 0; S[j] != '\0'; j++ ) { if (S[j] == '$') { for (size_t k = 0; name[k] != '\0'; k++) { buffer[i++] = name[k]; } } else { buffer[i++] = S[j]; } } buffer[i] = '\0'; std:: cout << buffer << std::endl; }
Its output to the console:
Hello John Shepard. What are you John Shepard going to say?
As you can see, the program does not use any functions to get the result.