The task itself:

Given a string, replace the $ character in it with the name entered by the user.

 #include "stdafx.h" #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { setlocale(LC_ALL, "rus"); int d,p=0,d1; char S[100]; char name[20]; char bufer[250]; cin.getline(name, 20); cin.getline(S, 20); d = strlen(S); d1 = strlen(name); for (int i = 0; i <= d; i++) if (S[i] == '$') { for(int g = 0; i <= d1; i++) bufer[p] = name[g]; p = p + d1; } else { p++; bufer[p] = S[i]; } cout << bufer<<endl; system("pause"); return 0; } 

Closed due to the fact that off-topic participants Kromster , αλεχολυτ , Harry , aleksandr barakin , Alexey Shimansky 22 Dec '16 at 22:14 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Kromster, αλεχολυτ, Harry, aleksandr barakin, Alexey Shimansky
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • format the code - Sublihim
  • Questions asking for help with debugging (“why does this code not work?”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question. Questions without an explicit description of the problem are useless for other visitors. See How to create a minimal, self-contained and reproducible example . - Denis

2 answers 2

Change to

 void replace(string&s, const string& rep) { for(size_t pos = s.find('$'); pos != string::npos; pos = (s.replace(pos,1,rep), s.find('$'))); } int main(int argc, const char * argv[]) { string s = "Hello, $ and $!"; string rep = "bro"; replace(s,rep); cout << s << endl; } 

and do not suffer :)

If itch through char* and guarantee that the buffer is enough, then

 void replace(char * buf, const char * rep) { char * c; int len = strlen(rep); while(c = strchr(buf,'$')) { memmove(c+len,c+1,strlen(c+1)+1); memcpy(c,rep,len); } } int main(int argc, const char * argv[]) { char buf[256] = "Hello, $ and $!"; char * rep = "bro"; replace(buf,rep); cout << buf << endl; } 

Both options do NOT handle recursive $ - i.e. in its substitution should not be!

    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.