C program to find last occurrence of a word in given string

Write a C program to find last occurrence of a word in given string using loop. How to find last occurrence of a word in given string using loop in C programming. Logic to search last index of a word in given string in C programming.

Example

Input

Input string: I love programming. I love Codeforwin.
Input word: love

Output

'love' is found at index: 22

Required knowledge

Basic C programming, Loop, String

Must know -

Logic to search last occurrence of a word in string

Logic to search last occurrence of a word is almost similar to searching first occurrence of a word in string. Just with last occurrence of a word do not terminate loop if first occurrence is found.
Below is the step by step descriptive logic to search last occurrence of a word in given string.

  1. Input string from user, store it in some variable say str.
  2. Input word to be searched from user, store it in some other variable say word.
  3. Initialize a variable to store last matched index of word in given string, say index = -1. I have initially assumed that given word does not exists in string hence initialized with -1.
  4. Run a loop from start of the string str to end.
  5. Inside loop, for each character in word match the rest of characters with str. If all characters in word and str matches then, update index with current match position.
  6. Finally, check if(index == -1), then the given word does not exists in string.

Program to find last occurrence of word in string

/**
 * C program to find last occurrence of a word in given string
 */

#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100 // Maximum string size

int main()
{
    char str[MAX_SIZE];
    char word[MAX_SIZE];
    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


    /*
     * Runs a loop from starting index of string to
     * length of string - word length
     */
    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: I love programming. I love Codeforwin.
Enter any word to search: love

Last index of 'love' = 22

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>