How do the days of week in C ? (String to Int) ???

Joined
Oct 9, 2024
Messages
1
Reaction score
0
Example: input is string "Wednesday", and program will output the number of this day of week (here 4 )

How do that?
 
Joined
Oct 9, 2024
Messages
2
Reaction score
0
#include <stdio.h>
#include <string.h>

int day_of_week(char *day_name) {
/*
This function takes a day of the week as a string and returns its
corresponding number.

Args:
day_name: The name of the day of the week.

Returns:
The number of the day of the week, where Monday is 1 and Sunday is 7,
or 0 if the day name is invalid.
*/

char *days[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
int num_days = sizeof(days) / sizeof(days[0]);

for (int i = 0; i < num_days; i++) {
if (strcmp(day_name, days) == 0) {
return i + 1;
}
}

return 0; // Invalid day name
}

int main() {
char day_name[] = "Wednesday";
int day_number = day_of_week(day_name);

if (day_number > 0) {
printf("%s is day number %d\n", day_name, day_number);
} else {
printf("Invalid day name: %s\n", day_name);
}

return 0;
}
 
Joined
Sep 21, 2022
Messages
168
Reaction score
24
The number of days in the week is always 7.

int num_days = sizeof(days) / sizeof(days[0]);

It would be safe to use a constant here.

Just my opinion.
 
Joined
Oct 9, 2024
Messages
2
Reaction score
0
#include <stdio.h>
#include <string.h>

#define NUM_DAYS 7

// Function to determine the nth occurrence of a specific day in a leap year
int nth_day_of_week(char *day_name, int nth) {
char *days[NUM_DAYS] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

// Find the index of the requested day
int day_index = -1;
for (int i = 0; i < NUM_DAYS; i++) {
if (strcmp(day_name, days) == 0) {
day_index = i;
break;
}
}

if (day_index == -1 || nth <= 0) {
return 0; // Invalid day name or nth value
}

// Calculate the nth occurrence
int occurrence = 0;
for (int day_of_year = 1; day_of_year <= 366; day_of_year++) {
// Check if the current day is the target day of the week
if ((day_of_year + 1) % NUM_DAYS == (day_index + 1) % NUM_DAYS) { // 1 Jan 2024 is a Monday
occurrence++;
if (occurrence == nth) {
return day_of_year; // Return the day of the year
}
}
}

return 0; // Not enough occurrences
}

int main() {
char day_name[] = "Wednesday";
int nth = 2; // Change this to find the 1st, 2nd, etc. occurrence
int day_of_year = nth_day_of_week(day_name, nth);

if (day_of_year > 0) {
printf("The %d occurrence of %s in 2024 is on day %d of the year.\n", nth, day_name, day_of_year);
} else {
printf("Invalid input or not enough occurrences of %s.\n", day_name);
}

return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top