Q:

Replacing a substring in a string - c

I'm trying to do a program which finds a substring in a string and replaces it with another substring entered by user. My code doesn't give a compile or run-time error, but it just doesn't work. I put printfs in the while loop which I wrote a comment line near it, and the program doesn't go into first if -I put another comment line near it. It prints a, h and i. The other parts in loop aren't working. Here's my code: 
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

char *findAndReplace(char *sentence, char *word1, char *word2);

void main()
{
char sentence[1000];
char word1[200];
char word2[200];
int length;

printf("Please enter a sentence: ");
gets(sentence);

printf("Please write the word to be replaced: ");
gets(word1);

printf("Please write the word to be put instead: ");
gets(word2);

findAndReplace(sentence, word1, word2);

system("pause");

}

char* findAndReplace(char *sentence, char *word1, char *word2)
{
char *search, *tempString[1000];
int a, b, c, d, i = 0, j, sentenceLength, word1Length, searchLength;

sentenceLength = strlen(sentence);
printf("Length of %s is %d\n", sentence, sentenceLength);

printf("Finding ");
puts(word1);

search = strstr(sentence, word1);
searchLength = strlen(search);
word1Length = strlen(word1);

strcpy(tempString, sentence);

if(search != NULL)
{
    printf("Starting point: %d\n", sentenceLength - searchLength);
}
else
{
    printf("Eşleşme bulunamadı.\n");
}
j = 0;
while(j < sentenceLength + 1)     //This loop
{
    printf("a");
    if(word1[i] == tempString[j])
    {
        printf("b");

        if(i == word1Length)
        {
            c = j;
            printf("c");

            for(d = 0; d < word1Length; d++)
            {
                tempString[c - word1Length + d + 1] = word2[d];
                printf("d");
            }

            i = 0;
            j++;
            printf("e");
        }

        else
        {      printf("f");
            i++;
            j++;
        }
        printf("g");
    }

    else{
        printf("h");
        i = 0;
        j++;
    }
    printf("i");
}
puts(tempString);
}

A:

You've made a decent start, but you're making this a lot harder than it needs to be.  One way to minimize errors is to rely on standard library functions when there are any that do the work you need done.  For example:
char tempString[1000];
char *search;

search = strstr(sentence, word1);
if (search) {
    ptrdiff_t head_length = search - sentence;
    int sentence_length = strlen(sentence);
    int word1_length = strlen(word1);
    int word2_length = strlen(word2);

    if (sentence_length + word2_length - word1_length < 1000) {
        /* construct the modified string */
        strncpy(tempString, sentence, head_length);
        strcpy(tempString + head_length, word2);
        strcpy(tempString + head_length + word2_length, search + word1_length);
        /* copy it over the original (hope it doesn't overflow!) */
        strcpy(sentence, tempString);
    } else {
        /* oops! insufficient temp space */
    }
} /* else the target word was not found */

That covers only the search / replacement bit, fixing the error in tempString's type first pointed out by iharob.  Also, it replaces only the first occurrence of the target word, as the original code appeared to be trying to do.

