2. To-Do List Application
Description: Build a simple command-line to-do list application that allows users to add, view, and delete tasks.
Key Features:
- Add tasks with a description.
- View all tasks.
- Delete tasks by index.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TASKS 100
#define TASK_LENGTH 100
char tasks[MAX_TASKS][TASK_LENGTH];
int task_count = 0;
void add_task(const char *task) {
if (task_count < MAX_TASKS) {
strcpy(tasks[task_count], task);
task_count++;
} else {
printf("Task list is full!\n");
}
}
void view_tasks() {
if (task_count == 0) {
printf("No tasks available.\n");
return;
}
for (int i = 0; i < task_count; i++) {
printf("%d: %s\n", i + 1, tasks[i]);
}
}
void delete_task(int index) {
if (index < 1 || index > task_count) {
printf("Invalid task number!\n");
return;
}
for (int i = index - 1; i < task_count - 1; i++) {
strcpy(tasks[i], tasks[i + 1]);
}
task_count--;
}
int main() {
int choice;
char task[TASK_LENGTH];
while (1) {
printf("\n1. Add Task\n2. View Tasks\n3. Delete Task\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); // Consume newline
switch (choice) {
case 1:
printf("Enter task: ");
fgets(task, TASK_LENGTH, stdin);
task[strcspn(task, "\n")] = 0; // Remove newline
add_task(task);
break;
case 2:
view_tasks();
break;
case 3:
printf("Enter task number to delete: ");
int task_num;
scanf("%d", &task_num);
delete_task(task_num);
break;
case 4:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
No comments:
Post a Comment
If you have any doubts, please let me know