You need to write such a program: The user must enter a sentence, a word that he wants to replace in the sentence, and a word which he wants to replace. Something similar happened, can someone tell you what and where to fix

#define SIZE 80 void newsentence(char a[], char b[], char c[], int n, int k); void array(int f, int s, int g, char buff[], char b[], char c[], int n, int k); int main() { char a[SIZE], b[SIZE], c[SIZE]; int k; printf("Enter the sentence: "); gets(a); printf("\nEnter the word which u want to replace: "); scanf("%s",b); printf("\nEnter the new word: "); scanf("%s",c); int n =0; char *temp; temp =a; while((temp =strstr(temp,b))!= NULL){ n++; temp++; k = strstr(temp,b)-b; } printf("The word appears %d times",n); newsentence(a,b,c,n,k); } void newsentence(char a[], char b[], char c[], int n, int k) { int f, s, g; char buff; f=strlen(a); s=strlen(b); g=strlen(c); strcpy(buff,a); array(f,s,g, buff,b,c,n,k); } void array(int f, int s, int g, char buff[], char b[], char c[], int n, int k) { int i,j; int q; q = k +s - 1; for(j = 0; k<=q ;k++, j++) { if(strcmp(buff[k],b[j])==0){ strcmp(buff[k],c[i]); } } for(i = 0; i<SIZE;i++){ printf("Yout new string is: %c",buff[i]); } } 

Closed due to the fact that off-topic participants Kromster , 0xdb , Enikeyschik , aleksandr barakin , Kirill Stoianov 16 December '18 at 14:09 .

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, 0xdb, Enikeyschik, aleksandr barakin, Kirill Stoianov
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    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. - Kromster
  • one
    what and where you need to fix - what is your compiler? The best place to start is to eliminate the comments issued by the compiler. the 'gets' function is dangerous and should not be used etc. - mkkik
  • "the word that he wants to replace " - who was he standing for? - freim
  • I changed the program, I hope it will be clearer - Aimin
  • So what's wrong with this program? - Chad

1 answer 1

Well, I sketched on my knee here ...

 char * replace(const char* src, const char* old, const char* new) { // Ищем количество вхождений old в src int count = 0; const char * c = src; while((c = strstr(c,old)) != NULL) { ++count; ++c; } // Вычисляем новую длину предложения int len = strlen(src) + count*(strlen(new) - strlen(old)) + 1; // Выделяем память char * s = malloc(len); // Указатели на старую строку const char * b = src; c = src; // На новую строку (текущее место копирования) char * e = s; while((c = strstr(c,old)) != NULL) { // Копируем кусок строки до старого слова memcpy(e,b,cb); // и новое слово memcpy(e+(cb),new,strlen(new)); e += (cb) + strlen(new); b = c + strlen(old); c = b; } strcpy(e,b); return s; } 

See, understand ...