Here is an example of a C program that filters data from a dictionary of posts using a given keyword for the text or title:
This program uses a struct called "Post" to define the structure of the data, which includes a text field and a title field. An array of struct Post is defined, and it includes several example posts.
The program uses a while loop to continually prompt the user for a keyword to search for. The scanf function is used to read the keyword entered by the user. Inside the while loop, a for loop iterates through the array of posts, and for each post, it checks if the keyword is present in either the text field or the title field using the strstr() function. If the keyword is found in either field, the title of the post is printed to the console.
You can run this program on your machine and it will not end after 1 attempt, you need to end it manually by closing the console.
#include <string.h>
struct Post {
char text[1000];
char title[100];
};
int main() {
struct Post posts[] = {
{"This is the first post text", "First post title"},
{"This is the second post text", "Second post title"},
{"This is the third post text", "Third post title"},
{"This is the fourth post text", "Fourth post title"},
{"This is the fifth post text", "Fifth post title"}
};
int numPosts = sizeof(posts) / sizeof(struct Post);
char keyword[100];
while (1) {
printf("Enter keyword: ");
scanf("%s", keyword);
printf("Posts containing keyword '%s':\n", keyword);
for (int i = 0; i < numPosts; i++) {
if (strstr(posts[i].text, keyword) || strstr(posts[i].title, keyword)) {
printf("%s\n", posts[i].title);
}
}
}
return 0;
}
First, the program includes the standard input/output library (stdio.h) and the string library (string.h), which are necessary for the program to use input/output and string manipulation functions.
The program then defines a struct called "Post" which includes two fields, "text" and "title", both are defined as character arrays of size 1000 and 100 respectively.
In the main function, an array of struct Post is defined, and it includes several example posts. The number of posts is calculated by taking the size of the whole array of posts and dividing it by the size of one struct Post.
The program then declares a variable called "keyword" which is an array of characters of size 100.
The program enters a while loop where it continually prompts the user for a keyword to search for. The scanf function is used to read the keyword entered by the user.
Inside the while loop, a for loop iterates through the array of posts, and for each post, it checks if the keyword is present in either the text field or the title field using the strstr() function. If the keyword is found in either field, the title of the post is printed to the console using the printf function.
The program will continue to prompt the user for a keyword until the program is manually closed.
Finally, the program returns 0, which is a standard return value indicating that the program executed successfully.
Comments
Post a Comment