Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views4 pages

Practical 3

The document outlines a program aimed at checking if a string contains a specified keyword using various methods, including direct string comparison, hashing for efficiency, and regular expressions. It provides algorithms for each method and includes a practical implementation in C code. The program prompts the user for a string and a keyword, checks for the keyword's presence, and outputs the result.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Practical 3

The document outlines a program aimed at checking if a string contains a specified keyword using various methods, including direct string comparison, hashing for efficiency, and regular expressions. It provides algorithms for each method and includes a practical implementation in C code. The program prompts the user for a string and a keyword, checks for the keyword's presence, and outputs the result.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Practical 3

AIM: Write a program to check whether a string include keyword or


not.
Theory: Checking whether a string includes a keyword involves
comparing a given string against a set of predefined keywords. The
theoretical basis for this operation lies in string matching algorithms
and data structures.
1.Direct String Comparison:
The most straightforward approach involves iterating through a list or
array of known keywords and comparing each keyword with the input
string or a substring of the input string.
Algorithm:
Define a collection (e.g., array, list, set) containing all the keywords to
be checked.
For each keyword in the collection:
a. Compare the keyword with the entire input string (for exact matches)
or with substrings of the input string (for keyword presence).
b. If a match is found, the string includes the keyword. Practical
Implementation.
2. Hashing for Efficiency:
For larger sets of keywords or frequent checks, hashing can improve
performance.
Algorithm:
Hash all the keywords into a hash set (or similar data structure).
Tokenize the input string into individual words or relevant units.
For each token, check if it exists in the pre-computed hash set of
keywords.
3.Regular Expressions:
Regular expressions provide a powerful and flexible way to define
complex patterns, including multiple keywords, within a single search
operation.
Algorithm:
Construct a regular expression pattern that matches any of the desired
keywords.
Apply the regular expression search operation on the input string.

Program:
#include <stdio.h>
#include <string.h>
int check_keyword(char* string, char* keyword) {
if (strstr(string, keyword) != NULL) {
Return 1;
} else {
Return 0;
}
}
int main() {
char string[100];
char keyword[20];
printf(“Enter a string: “);
fgets(string, sizeof(string), stdin);
printf(“Enter a keyword: “);
fgets(keyword, sizeof(keyword), stdin);
// Removing newline character from input
string[strcspn(string, “\n”)] = ‘\0’;
keyword[strcspn(keyword, “\n”)] = ‘\0’;
int result = check_keyword(string, keyword);
if (result) {
printf(“The string includes the keyword.\n”);
} else {
printf(“The string does not include the keyword.\n”);
}
Return 0;
}
Output:

You might also like