medium
2 views

Print February Calendar

Generate a calendar for February of a given year, starting from a specified day of the week.

Understand the Problem

Problem Statement

Given a specific year Y and the starting day of the year D, your task is to print the February month calendar for that year.

The calendar must be displayed in a grid format with columns representing the days of the week (Sunday through Saturday). Days that fall outside February should be represented by an asterisk (*).

Note: You must determine if February has 28 or 29 days based on whether the given year is a leap year.

Constraints

  • Year Y must be a valid positive integer (e.g., 1000 ≤ Y ≤ 9999)
  • Starting day D must be one of: MON, TUE, WED, THU, FRI, SAT, SUN
  • Leap year rule: A year is a leap year if it is divisible by 400, or divisible by 4 but not by 100
  • February has 28 days in a common year and 29 days in a leap year
  • Output must display exactly 7 columns (S M T W T F S) and 5 or 6 rows depending on the calendar layout
  • All non-February days must be displayed as '*'

Examples

Example 1
Input
2018 MON
Output
S M T W T F S
* * * * 1 2 3 
4 5 6 7 8 9 10 
11 12 13 14 15 16 17 
18 19 20 21 22 23 24 
25 26 27 28 * * * 
Explanation

Year 2018 is not a leap year, so February has 28 days. The year starts on Monday (MON). The calendar shows February 1st starting in the Friday position (5th column), with asterisks representing non-February days before and after the month.

Example 2
Input
1597 WED
Output
S M T W T F S
* * * * * * 1 
2 3 4 5 6 7 8 
9 10 11 12 13 14 15 
16 17 18 19 20 21 22 
23 24 25 26 27 28 * 
Explanation

Year 1597 is not a leap year (28 days in February). The year starts on Wednesday (WED). February 1st appears in the Sunday position (1st column), with asterisks before the 1st and after the 28th.

Solution

#include <stdio.h>
#include <string.h>

int main() {
    int year, month, s, t = 0, v;
    char day[4];

    // Read the year and starting day from the user
    scanf("%d %s", &year, day);

    // Determine the number of days in February based on leap year
    month = (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)) ? 29 : 28;

    // Mapping of starting day to the corresponding day index
    // Index: 1=Mon, 2=Tue, 3=Wed, 4=Thu, 5=Fri, 6=Sat, 7=Sun
    if (strcmp(day, "MON") == 0) s = 1;
    else if (strcmp(day, "TUE") == 0) s = 2;
    else if (strcmp(day, "WED") == 0) s = 3;
    else if (strcmp(day, "THU") == 0) s = 4;
    else if (strcmp(day, "FRI") == 0) s = 5;
    else if (strcmp(day, "SAT") == 0) s = 6;
    else if (strcmp(day, "SUN") == 0) s = 7;

    // 2D array to represent the calendar (6 weeks x 7 days)
    int a[8][8] = {0};

    // Populate the calendar with dates
    for (int i = 1; i < 6; i++) {
        if (i == 1) {
            // First week: fill from starting day position
            for (int j = s; j < 8; j++) {
                a[i][j] = ++t;
            }
        } else {
            // Subsequent weeks: fill all positions until month limit
            for (int j = 1; j < 8; j++) {
                if (t < month) {
                    a[i][j] = ++t;
                }
            }
        }
    }

    // Print the calendar header
    printf("S M T W T F S\n");

    // Determine the number of weeks to print (5 or 6)
    v = (a[5][1] != 0) ? 6 : 5;

    // Print the calendar content
    for (int i = 1; i < v; i++) {
        for (int j = 1; j < 8; j++) {
            if (a[i][j] != 0) {
                printf("%d ", a[i][j]);
            } else {
                printf("* ");
            }
        }
        printf("\n");
    }

    return 0;
}
Time:O(1) - The algorithm performs a fixed number of operations regardless of input size. The calendar grid is always 6x7 maximum.
Space:O(1) - Uses a fixed-size 8x8 array and a small character buffer, independent of input values.
Approach:

C Solution Explanation:

1. Input Handling: Uses scanf to read integer year and string day from standard input.

2. Leap Year Check: Applies the standard leap year formula to determine February length.

3. Day Mapping: Converts day string to numeric index using strcmp comparisons. The mapping used is: MON=1, TUE=2, WED=3, THU=4, FRI=5, SAT=6, SUN=7.

4. Calendar Array: Creates an 8x8 integer array (using indices 1-7) initialized to zero.

5. Grid Population: Uses nested loops to fill the calendar. First week starts at the calculated day position, subsequent weeks fill completely.

6. Output Generation: Prints header, then iterates through the filled array, printing numbers or asterisks.

Key Features:

  • Uses character arrays for string comparison
  • Employs zero-initialized arrays for empty day detection
  • Handles both 28-day and 29-day February scenarios

Visual Explanation

Loading diagram...