#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_COURSES 100
#define MAX_LEN 100
#define MAX_FAVS 10
// Structure for Course
typedef struct {
char name[MAX_LEN];
char category[MAX_LEN];
int popularity;
} Course;
// Structure for User
typedef struct {
int userID;
char favorites[MAX_FAVS][MAX_LEN];
int favCount;
} User;
// Global course list
Course courses[MAX_COURSES];
int courseCount = 0;
// Function to convert string to lowercase (for case-insensitive match)
void toLowerStr(char *s) {
for (int i = 0; s[i]; i++) s[i] = tolower(s[i]);
}
// Function to load courses (hardcoded for simplicity)
void loadCourses() {
strcpy(courses[0].name, "Data Structures");
strcpy(courses[0].category, "Computer Science");
courses[0].popularity = 1200;
strcpy(courses[1].name, "Calculus I");
strcpy(courses[1].category, "Mathematics");
courses[1].popularity = 950;
strcpy(courses[2].name, "Intro to AI");
strcpy(courses[2].category, "Computer Science");
courses[2].popularity = 1500;
strcpy(courses[3].name, "Physics Basics");
strcpy(courses[3].category, "Physics");
courses[3].popularity = 800;
courseCount = 4;
}
// Function to sort courses by popularity (descending)
void sortCourses(Course arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[j].popularity > arr[i].popularity) {
Course temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
int main() {
loadCourses();
char keyword[MAX_LEN];
printf("Enter course search keyword: ");
fgets(keyword, MAX_LEN, stdin);
keyword[strcspn(keyword, "\n")] = '\0'; // remove newline
char keyLower[MAX_LEN];
strcpy(keyLower, keyword);
toLowerStr(keyLower);
// Find matching courses
Course matched[MAX_COURSES];
int matchedCount = 0;
for (int i = 0; i < courseCount; i++) {
char nameLower[MAX_LEN];
strcpy(nameLower, courses[i].name);
toLowerStr(nameLower);
if (strstr(nameLower, keyLower)) {
matched[matchedCount++] = courses[i];
}
}
if (matchedCount == 0) {
printf("\nNo courses found for '%s'\n", keyword);
return 0;
}
printf("\nMatching Courses:\n");
for (int i = 0; i < matchedCount; i++) {
printf("%s (%s) - Popularity: %d\n",
matched[i].name, matched[i].category, matched[i].popularity);
}
// Get user preferences
User user;
printf("\nEnter User ID: ");
scanf("%d", &user.userID);
printf("How many favorite categories? ");
scanf("%d", &user.favCount);
getchar(); // flush newline
for (int i = 0; i < user.favCount; i++) {
printf("Enter favorite category %d: ", i + 1);
fgets(user.favorites[i], MAX_LEN, stdin);
user.favorites[i][strcspn(user.favorites[i], "\n")] = '\0';
toLowerStr(user.favorites[i]);
}
// Filter courses by favorites
Course filtered[MAX_COURSES];
int filteredCount = 0;
for (int i = 0; i < matchedCount; i++) {
char catLower[MAX_LEN];
strcpy(catLower, matched[i].category);
toLowerStr(catLower);
for (int j = 0; j < user.favCount; j++) {
if (strcmp(catLower, user.favorites[j]) == 0) {
filtered[filteredCount++] = matched[i];
break;
}
}
}
if (filteredCount == 0) {
printf("\nNo matching courses found in your favorite categories.\n");
return 0;
}
// Sort filtered courses by popularity
sortCourses(filtered, filteredCount);
printf("\nPersonalized Ranked Courses:\n");
for (int i = 0; i < filteredCount; i++) {
printf("%s (%s) - Popularity: %d\n",
filtered[i].name, filtered[i].category, filtered[i].popularity);
}
return 0;
}