Click here to Skip to main content
15,612,595 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need help with an exercise in C. This is what I tried to do, but it doesn't work.

int main()
    int i, leng;
    char phrase[DIM], word[DIM], word2[DIM];

    printf("Write a sentence\n>>");
    fgets(phrase, sizeof(phrase), stdin);

    printf("Enter the word you want to replace in the sentence\n>> ");
    fgets(word, sizeof(word), stdin);

    printf("What do you want to replace it with?\n>> ");
    fgets(word2, sizeof(word2), stdin);

    leng = strlen(phrase);

    for(i = 0; i < leng; i++){
        if(phrase[i] == word){
            phrase[i] = word2;
    printf("%s", phrase);

When I start it, these warnings appear:

assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]

comparison between pointer and integer

What I have tried:

I tried to use strcmp, but i don't know how to use it with this exercise
Updated 6-Jan-21 20:20pm

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"
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.
Share this answer
Member 15039311 6-Jan-21 19:54pm    
Can you do an example with code?
k5054 7-Jan-21 10:36am    
I could, but I think you would get more out of it if you tried to solve it yourself. Take a look at documentation for string ops (strcpy, strcmp, etc), and give it a try! If you still have problems, post another question.
Make sure you understand how strings work in C and then use the sample code in this page strstr - C++ Reference[^] as starting point (note: your task is a bit more difficult).
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900