You are on page 1of 5

PROGRAME - 1:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

// Structure to represent a day in the calendar

struct Day {

char *name;

int date;

char *activity;

};

// Function to create a day and populate its fields

struct Day createDay() {

struct Day day;

day.name = (char *)malloc(20 * sizeof(char)); // Allocating memory for the name

printf("Enter the name of the day: ");

scanf("%s", day.name);

printf("Enter the date: ");

scanf("%d", &day.date);

day.activity = (char *)malloc(100 * sizeof(char)); // Allocating memory for the activity description

printf("Enter the activity for the day: ");

scanf(" %[^\n]s", day.activity);

return day;

}
// Function to check for duplicate entries

int isDuplicate(struct Day calendar[7], struct Day newDay, int numDays) {

for (int i = 0; i < numDays; i++) {

if (strcmp(calendar[i].name, newDay.name) == 0) {

return 1; // Duplicate found

return 0; // No duplicate

// Function to read data for the calendar from the keyboard

void read(struct Day calendar[7]) {

int numDays = 0;

while (numDays < 7) {

printf("\nEnter details for day %d:\n", numDays + 1);

struct Day newDay = createDay();

if (!isDuplicate(calendar, newDay, numDays)) {

calendar[numDays] = newDay;

numDays++;

} else {

printf("Duplicate entry. Please enter a different day.\n");

free(newDay.name);

free(newDay.activity);

// Function to display the calendar

void display(struct Day calendar[7]) {

printf("\nCalendar for the week:\n");


for (int i = 0; i < 7; i++) {

printf("Day %d: %s, Date: %d\n", i + 1, calendar[i].name, calendar[i].date);

printf("Activity: %s\n\n", calendar[i].activity);

// Function to free dynamically allocated memory

void freeMemory(struct Day calendar[7]) {

for (int i = 0; i < 7; i++) {

free(calendar[i].name);

free(calendar[i].activity);

int main() {

struct Day calendar[7];

printf("Create a weekly calendar:\n");

read(calendar);

display(calendar);

// Free allocated memory

freeMemory(calendar);

return 0;

OUTPUT:

Create a weekly calendar:

Enter details for day 1:


Enter the name of the day: mon

Enter the date: 1

Enter the activity for the day: yogo

Enter details for day 2:

Enter the name of the day: tue

Enter the date: 2

Enter the activity for the day: game

Enter details for day 3:

Enter the name of the day: wed

Enter the date: 3

Enter the activity for the day: swimming

Enter details for day 4:

Enter the name of the day: thur

Enter the date: 4

Enter the activity for the day: running

Enter details for day 5:

Enter the name of the day: fri

Enter the date: 5

Enter the activity for the day: study

Enter details for day 6:

Enter the name of the day: sat

Enter the date: 6

Enter the activity for the day: gardening

Enter details for day 7:

Enter the name of the day: sun


Enter the date: 7

Enter the activity for the day: sleeping

Calendar for the week:

Day 1: mon, Date: 1

Activity: yogo

Day 2: tue, Date: 2

Activity: game

Day 3: wed, Date: 3

Activity: swimming

Day 4: thur, Date: 4

Activity: running

Day 5: fri, Date: 5

Activity: study

Day 6: sat, Date: 6

Activity: gardening

Day 7: sun, Date: 7

Activity: sleeping

You might also like