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

Joined
Oct 8, 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 20, 2022
Messages
230
Reaction score
38
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;
}
 
Joined
Jan 1, 2025
Messages
12
Reaction score
0
I wouldn't let the user type in the name. I'd have a drop down list or a check box setup for selection by mouse.

Can you do ordinal types in C? You can use them in loops.
In Pascal it would be:

Type
DaysofWeekType = (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday);

Const
Days :array[Sunday..Saturday] of string = ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');

Var
Day : DaysofWeekType;

begin
day := Sunday; (* The "day" can be converted to a number with ord(day) *)

Writeln(DaysofWeek[Day]);
Writeln(Ord(Day));

For day := Friday to Saturday do writeln(Days[Day]);

end.

The output would be
Sunday
0
Friday
Saturday
 
Last edited:

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

No members online now.

Forum statistics

Threads
474,260
Messages
2,571,038
Members
48,768
Latest member
first4landlord

Latest Threads

Top