char phrase[DIM], word[DIM], word2[DIM];
if(phrase[i] == word)
phrase[i] = word2;
phrase, word
and
word2
are all single dimension arrays of characters. Thus the expression
phrase[i] == word
attempts to compare the single character at phrase[i] with the contents of the variable word e.g.
'c' == "hello"
Similarily
phrase[i] = word2
attempts to assign a string of characters to a single char.
In each case, the C compiler is trying to assign or compare the
address of the word or word2 varialbe to the single char contained in phrase[i].
What you want is to use
strcmp()
, or perhaps
strncmp()
to compare strings. You'll then need to figure out how to paste the replacement string into the original one. You might be tempted to use
strcpy()
, and it can be done using that, but you need to be careful. Consider, if your original phrase is "all dogs go to heaven" and you want to replace "dogs" with "elephants", you want to make sure you don'e end up with "all elephantso heaven".
You may also want to look at the man page for
strstr()
, which might help you find the string to replace. But again, be careful, since, if your starting phrase is "six yelling yellers yell loudly", and you want to replace "yell" with "shout", strstr() doesn't care about word delimiters, so will find the first occurence of "yell", so you could end up with "six shouting yellers yell loudly", which might not be what is wanted.
You might also want to look at the man page for
strtok()
, which might help with analyzing the original phrase.