Lextree Simply the best software for managing legal entities and documents.
The Berkman Letter Practical analysis and commentary for business, law, and more.

c program to Replace a word in a text by another given word

0
stars
1
answer
26
views
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 
Answers
0
stars
Check out the answers to this same question on Stack Overflow: https://stackoverflow.com/questions/15465755/replacing-a-word-in-a-text-by-another-given-word