Find the last occurrence of a word in a given string with C.

#include <stdio.h>
#include <string.h>


int main()
{
    char str[100];
    char word[100];
    int i, j, index, found;
    int strLen, wordLen;

    /* Input string and word from user */
    printf("Enter any string: ");
    gets(str);
    printf("Enter any word to search: ");
    gets(word);

    index = -1;
    strLen = strlen(str);   // Find length of string
    wordLen = strlen(word); // Find length of word

    for (i = 0; i <= strLen - wordLen; i++)
    {
        // Match word at current position
        found = 1;
        for (j = 0; j < wordLen; j++)
        {
            // If word is not matched
            if (str[i + j] != word[j])
            {
                found = 0;
                break;
            }
        }

        // If word have been found then store the current found index
        if (found == 1)
        {
            index = i;
        }
    }

    if (index == -1)
    {
        printf("\n'%s' not found.", word);
    }
    else
    {
        printf("\nLast index of '%s' = %d", word, index);
    }

    return 0;
}

Output


Enter any string : Learn With Reza
Enter any word to search : Reza

Last index of 'Reza' = 11