int replace_text (Document *doc, const char *target, const char *replacement) The function will replace the text target with the text replacement everywhere it appears in the docu- ment. You can assume the replacement will not generate a line that exceeds the maximum line length; also you can assume the target will not be the empty string The function will return FAILURE if doc, target or replacement are NULL; otherwise the function will return SUCCESS. #de fine DOCUMENT H #de fine MAX PARAGRAPH LINES 20 #de fine MAX PARAGRAPHS 15 #de fine MAX STR SIZE 80 #de fine HIGHLIGHT START STR "[" #define HIGHLIGHT END STR "ן" #de fine SUCCESS #de fine FAILURE - typedef struct { int number of 1ines: char lines[MAX PARAGRAPH LINES 1 [MAX STR SIZE + 11 \ Paragraph; typedef struct { char name [ MAX STR SIZE+ 11 int number of paragraphs; Paragraph paragraphs [MAX PARAGRAPHS]; } Document;

Respuesta :

Answer:

int replace(Document *doc, const char *target, const char *replacement){

int i, j, k;

int beginning;

char temp[MAX_STR_SIZE + 1] ;

 

char *beginning, *end, *occ, *line;

if(doc == NULL || target == NULL || replacement == NULL)

return FAILURE;

for(i = 0; i < doc->total_paragraphs; i++){

for(j = 0; j < doc->paragraphs[i]->total_lines; j++){

line = doc->paragraphs[i]->lines[j];

beginning = line;

end = beginning + strlen(line);

strcpy(temp, "");

while(beginning < end && (occ = strstr(beginning, target))!= NULL){

len = occ - beginning;

strncat(temp, beginning, len);

strcat(temp, replacement);

beginning = occ + strlen(target);

}

strcat(temp, beginning);

strcpy(doc->paragraphs[i]->lines[j], temp);

}

}

return SUCCESS;

}

Explanation:

  • Loop through total paragraphs and total lines.
  • Store the beginning and ending of paragraph in specific variables.
  • Copy the remainging chars .
  • Finally return SUCCESS.