i'm trying to write a program that accepts three strings: text, s, and r. The function searches string text for string s. if it finds string s, it should replace it with string r. i wrote all the code and it compiles but when i try and run it stops and says "segmentation fault"
i know it's something with my pointers but i can't figure it out for the life of me. i could definitely use some help...thanks
Code:
#include <stdio.h>
#include <string.h>
#define MAX 100
char search_and_replace(char *text,char *s, char *r);
int main(void)
{
char *text[MAX];
char *s[MAX];
char *r[MAX];
/* Find text string */
printf("Please enter a text string.\n");
gets(*text);
/* Find the search string */
printf("Please enter a search string.\n");
gets(*s);
/* Find the replacing string */
printf("Please enter a replace string.\n");
gets(*r);
search_and_replace(*text, *s, *r);
return(0);
}
char search_and_replace(char *text, char *s, char *r)
{
int next;
char length_text;
char length_s;
char diff;
length_text = strlen(text);
length_s = strlen(s);
diff = length_text - length_s;
for(next = 0; next <= diff; ++next);
{
if(strncmp(&text[next], s, length_s) == 0)
{
strcpy(&text[next], r);
printf("%s", text);
}
}
return(0);
}