How to find a word in a string in C

1 Answer

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

int main(void)
{
    char sentence[50] = "Linux c programming";
    char word[20] = "Linux";

    if (strstr(sentence, word) != NULL)
        printf("Found\n"); // Found
    else
        printf("NOT Found\n");
    
    if (strstr(sentence, "linux") != NULL)
        printf("Found\n"); 
    else
        printf("NOT Found\n"); // NOT Found
   
    // find also a part of the word
    if (strstr(sentence, "Lin") != NULL)
        printf("Found\n"); // Found
    else
        printf("NOT Found\n"); 
    
    return 0;
}


/*
run:

Found
NOT Found
Found

*/


answered Oct 25, 2014 by avibootz
edited Nov 8, 2025 by avibootz
...