Hello all, this is the last problem on a hmwk assignment of mine, and I am really getting frustrated over it. I am having probs writing my str_replace function...
Basically, what this program needs to do is the following:
Input sentence : This is very fun.
S (search) = very
R (replace) = not
Output sentence: This is not fun.
This is my thought process for this program:
-have it look through the string you give to the function
-if you find a character that matches the first character of the word to replace then keep checking to see if the whole word is there.
-If it is, then don't add that new word to a temp string, but add the rest and insert the replacement string.
Below is what I have up to now... my replace function may be really wrong, I honestly do not know for sure.... any help would be great. If anyone can walk me through the darn function that would help a lot too, I'm just confused.
Code:
#include<stdio.h>
#define MAX_LEN 100
int strlen(char *s)
{
char *p=s;
while(*p!='\0')
p++;
return p-s;
}
void strcopy(char *x, char *y)
{
int i=0;
while(*x!='\0') {
*y=*x;
x++;
y++;
}
*y='\0';
}
int strcomp(char *x, char *y)
{
while((*x==*y)&&(*x!='\0')&&(*y!='\0')) {
x++;
y++;
}
if(*x<*y)
return 1;
else if(*x==*y)
return 0;
else
return -1;
}
string_replace(char *orig_text, char *search, char *replace)
{
int *p, *temp, len;
char array;
p=orig_text;
*temp=array[0];
len=strlen(search);
while(p!='\0') {
if(*p==*search) {
temp=&temp[0];
for(i=1; i<=strlen(search); i++)
*temp=*p;
p++;
temp++;
strcomp(temp, search);
else if(*p!=*search)
p++;
/* I am really gettin confused w/ this function at this point... how do I start to check character by character, etc? */
}
}
}
main()
{
char original_str[MAX_LEN], s_str[MAX_LEN], r_str[MAX_LEN], outp_str[MAX_LEN];
int index;
printf("Original text:");
gets(original_str);
printf("s=:");
gets(s_str);
printf("r=:");
gets(r_str);
printf("New Sentence: %s %s, %s\n", original_str, s_str, r_str);
}