#include <stdio.h>
#include <string.h>
int main(void){
    char sen[100];
    char word[30];
    char rep[30];
    printf("Enter a sentence> ");
    gets(sen);
    printf("Enter a word to compare> ");
    gets(word);
    printf("Enter a word to replace with> ");
    gets(rep);
    int sen_len = strlen(sen);
    int word_len = strlen(word);
    char temp[30];
    char retSen[100];
    char rest[70];
    int i;
    int y=0;
    for(i=0; i<sen_len; i++){
        //Loop through characters in sentence until you reach a whitespace. ie, extract a word and put it in temp.
        if(sen[i] == ' '){
            //Compare the word with target word
            if(!strcmp(temp, word)){
                //In case of match, copy the part upto but not including the matching word and put it in the final string (retSen)
                strncpy(retSen, sen, sen_len-i);
            }else{
                //Clear temp and skip to next iteration
                y=0;
                temp[0] = '\0';
                continue;
            }
        }
        y++;
        //Repeatedly populate y with a word from sen
        temp[y] = sen[i];
    }
    int x=0;
    for(int j=i; j<sen_len; j++){
        //Populate another char array with the rest of the sentence after the matching word.
        rest[x] = sen[j];
    }
    //Join the part upto the matching word with the replacing word.
    strcat(retSen, rep);
    //Then join the rest of the sentence after the matching word to make the full string with the matching word replaced.
    strcat(retSen, rest);
    puts(retSen);
    return 0;
}
I'm trying to make a program that will take a sentence, a target word to replace and another word to replace the target with. This code isn't working. It just prints the replacing word + 'a' for some reason. How can I fix this code?
 
    