fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. #define MAX_COURSES 100
  7. #define MAX_LEN 100
  8. #define MAX_FAVS 10
  9.  
  10. // Structure for Course
  11. typedef struct {
  12. char name[MAX_LEN];
  13. char category[MAX_LEN];
  14. int popularity;
  15. } Course;
  16.  
  17. // Structure for User
  18. typedef struct {
  19. int userID;
  20. char favorites[MAX_FAVS][MAX_LEN];
  21. int favCount;
  22. } User;
  23.  
  24. // Global course list
  25. Course courses[MAX_COURSES];
  26. int courseCount = 0;
  27.  
  28. // Function to convert string to lowercase (for case-insensitive match)
  29. void toLowerStr(char *s) {
  30. for (int i = 0; s[i]; i++) s[i] = tolower(s[i]);
  31. }
  32.  
  33. // Function to load courses (hardcoded for simplicity)
  34. void loadCourses() {
  35. strcpy(courses[0].name, "Data Structures");
  36. strcpy(courses[0].category, "Computer Science");
  37. courses[0].popularity = 1200;
  38.  
  39. strcpy(courses[1].name, "Calculus I");
  40. strcpy(courses[1].category, "Mathematics");
  41. courses[1].popularity = 950;
  42.  
  43. strcpy(courses[2].name, "Intro to AI");
  44. strcpy(courses[2].category, "Computer Science");
  45. courses[2].popularity = 1500;
  46.  
  47. strcpy(courses[3].name, "Physics Basics");
  48. strcpy(courses[3].category, "Physics");
  49. courses[3].popularity = 800;
  50.  
  51. courseCount = 4;
  52. }
  53.  
  54. // Function to sort courses by popularity (descending)
  55. void sortCourses(Course arr[], int n) {
  56. for (int i = 0; i < n - 1; i++) {
  57. for (int j = i + 1; j < n; j++) {
  58. if (arr[j].popularity > arr[i].popularity) {
  59. Course temp = arr[i];
  60. arr[i] = arr[j];
  61. arr[j] = temp;
  62. }
  63. }
  64. }
  65. }
  66.  
  67. int main() {
  68. loadCourses();
  69.  
  70. char keyword[MAX_LEN];
  71. printf("Enter course search keyword: ");
  72. fgets(keyword, MAX_LEN, stdin);
  73. keyword[strcspn(keyword, "\n")] = '\0'; // remove newline
  74. char keyLower[MAX_LEN];
  75. strcpy(keyLower, keyword);
  76. toLowerStr(keyLower);
  77.  
  78. // Find matching courses
  79. Course matched[MAX_COURSES];
  80. int matchedCount = 0;
  81. for (int i = 0; i < courseCount; i++) {
  82. char nameLower[MAX_LEN];
  83. strcpy(nameLower, courses[i].name);
  84. toLowerStr(nameLower);
  85. if (strstr(nameLower, keyLower)) {
  86. matched[matchedCount++] = courses[i];
  87. }
  88. }
  89.  
  90. if (matchedCount == 0) {
  91. printf("\nNo courses found for '%s'\n", keyword);
  92. return 0;
  93. }
  94.  
  95. printf("\nMatching Courses:\n");
  96. for (int i = 0; i < matchedCount; i++) {
  97. printf("%s (%s) - Popularity: %d\n",
  98. matched[i].name, matched[i].category, matched[i].popularity);
  99. }
  100.  
  101. // Get user preferences
  102. User user;
  103. printf("\nEnter User ID: ");
  104. scanf("%d", &user.userID);
  105.  
  106. printf("How many favorite categories? ");
  107. scanf("%d", &user.favCount);
  108. getchar(); // flush newline
  109.  
  110. for (int i = 0; i < user.favCount; i++) {
  111. printf("Enter favorite category %d: ", i + 1);
  112. fgets(user.favorites[i], MAX_LEN, stdin);
  113. user.favorites[i][strcspn(user.favorites[i], "\n")] = '\0';
  114. toLowerStr(user.favorites[i]);
  115. }
  116.  
  117. // Filter courses by favorites
  118. Course filtered[MAX_COURSES];
  119. int filteredCount = 0;
  120. for (int i = 0; i < matchedCount; i++) {
  121. char catLower[MAX_LEN];
  122. strcpy(catLower, matched[i].category);
  123. toLowerStr(catLower);
  124.  
  125. for (int j = 0; j < user.favCount; j++) {
  126. if (strcmp(catLower, user.favorites[j]) == 0) {
  127. filtered[filteredCount++] = matched[i];
  128. break;
  129. }
  130. }
  131. }
  132.  
  133. if (filteredCount == 0) {
  134. printf("\nNo matching courses found in your favorite categories.\n");
  135. return 0;
  136. }
  137.  
  138. // Sort filtered courses by popularity
  139. sortCourses(filtered, filteredCount);
  140.  
  141. printf("\nPersonalized Ranked Courses:\n");
  142. for (int i = 0; i < filteredCount; i++) {
  143. printf("%s (%s) - Popularity: %d\n",
  144. filtered[i].name, filtered[i].category, filtered[i].popularity);
  145. }
  146.  
  147. return 0;
  148. }
  149.  
Success #stdin #stdout 0s 5316KB
stdin
45
stdout
Enter course search keyword: 
No courses found for '45'