I have a project assignment for class and this is what it wants (in C)
Description
Write a program that simulates the search-and-replace operation in a text editor. The program is to have only three function calls in “main”. The first function prompts the user to type a string of less than 80 characters. It then prompts the user to type the search substring of 10 or fewer characters. Finally, it prompts the user to type the replace substring of 10 or fewer characters. The second call is the search-and-replace function, which replaces all occurences of the search substring with the replace substring and creates a new string. If no occurrences are found, it returns the original string. Theoretically, the new string could be 800 characters long (80 identical characters replaced by 10 characters each). Your function must be able to handle overflow by using the “realloc” function to extend the new string when necessary. (Start with an output string of 80 characters and extend by 80 as necessary.) The search-and-replace function returns the address of the new string.
After the search-and-replace function returns, a print function prints the resulting string as a series of 80 character lines. If performs word-wrap. That is, a line can end only at a space. If there is no space in 80 characters, then print 79 characters and a hyphen, and continue on the next line. Write each called function using good structured programming techniques. It is expected to call subfunctions as necessary. Run the program at least three times:
a. First, run it with no substitutions in the original input.
b. Second, run it with two or more substitutions.
c. Finally, run it with substitutions that cause the output to be at least three lines, one of which requires a hyphen.
//this is what I have. it works but for some reason it only prints the original string for question c. ?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void meow();
void modify();
void print();
char r[10];
char str[80];
char s[10];
char ans[800];
int main() {
meow();
modify();
print();
return 0;
}
void meow() {
printf("\nEnter a string max 80 chars \n");
fgets(str, 80, stdin);
printf("\nEnter a search string max 10 chars \n");
fgets(s, 10, stdin);
printf("\nEnter a replace string max 10 chars \n");
fgets(r, 10, stdin);
}
void modify()
{
int i;
int j;
int c;
int m;
int k;
i = 0;
m = 0;
c = 0;
j = 0;
k = 0;
while ((str[c]) != '\0') {
//if (str[m] == s[i]) {
if (str[m] == s[i]) {
i++;
m++;
if (s[i] == '\0') {
for (k = 0; r[k] != '\0'; k++, j++) {
ans[j] = r[k];
i = 0;
c = m;
}
}
}
//}
else {
ans[j] = str[c];
j++;
c++;
m = c;
i = 0;
}
//}
ans[j] = '\0';
}
}
void print() {
int i, ansl;
ansl = strlen(ans);
if (ansl > 80) {
for (i = 0;i < 80;i++) {
printf("%c", ans[i]);
printf("-\n");
}
}
else {
printf("%s\n", ans);
}
}