Sunday, August 9, 2026

Course Description Keyword Search || C Lab Programs

Program 06: Search keyword in course description. 

Problem Statement:: Develop a C program that accepts a course description string and a keyword from the user. Search whether the keyword exists within the course description using appropriate string functions.

Problem Description:

  • Input: A string representing the course description (multiple words) and a string representing the keyword to search for.

  • Output: A message confirming if the keyword was found or not.

  • Constraints: Keyword search is case-sensitive; assume input strings are less than 256 characters.

  • Method: Use the standard C string function strstr() to check if the keyword is a substring of the description.

Pgm Logic:

  1. Start.

  2. Input the course description string (using a method that supports spaces).

  3. Input the keyword to search for.

  4. Use the strstr() function to search for the keyword within the description.

  5. If strstr() returns a non-NULL pointer, display: "Keyword '' found in the course description.".

  6. Otherwise, display: "Keyword '' not found in the course description.".

  7. Stop.

Program Code:

// Purpose: To search for a keyword within a multi-word course description string.

#include <stdio.h>

#include <string.h>


void main()

{

    char *description;

    char *keyword;


    printf("Enter the course description: ");

    // Using [^\n] to read the entire line including spaces until Enter is pressed

    scanf(" %[^\n]s", description);


    printf("Enter the keyword to search: ");

    scanf("%s", keyword);


    // strstr returns the address of the first occurrence or NULL if not found

    if (strstr(description, keyword))

    {

        printf("Keyword '%s' found in the course description.\n", keyword);

    }

    else

    {

        printf("Keyword '%s' not found in the course description.\n", keyword);

    }

}


Output: 

Enter the course description: Introduction to Computer Science 

Enter the keyword to search: Computer 

Keyword 'Computer' found in the course description.

RESULT: Thus the program has been executed and the output was verified.

Remarks: The key fix is using scanf(" %[^\n]s", description); or fgets(). The original manual code failed because standard %s stops at the first space, meaning "Introduction to Computer" would be truncated to just "Introduction". This corrected version was verified using the onlineGDB tool.

Program Explanation: This program utilizes the string.h library. The strstr() function is the core of the logic; it performs a linear search for the keyword inside the larger description string. If the keyword is present anywhere in the description, the function returns a pointer to its start, which evaluates to "true" in the if condition.


For all 2026 published C Lab Program posts Index page: click here

For all 2026 published articles list:click here

…till the next post, bye-bye & take care