find word in string in c code example

Example: search a word in a string in c

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

#include 
#include 
#define MAX_SIZE 100 // Maximum string size

int main()
{
    char str[MAX_SIZE];
    char word[MAX_SIZE];
    int i, j, found;

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

    /*
     * Run a loop from starting index of string to
     * length of string - word length
     */
    for(i=0; i

Tags:

Misc Example