5、编写一个函数,将数字0-6分别转换为Sunday,Monday,Tuesday,Wednsday,Thursday,Friday,Saturday。char * int
时间: 2023-09-18 19:11:23 浏览: 89
下面是一个示例函数,可以将数字转换为对应的星期几名称:
```c
char* int_to_weekday(int num) {
char* weekdays[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
if (num >= 0 && num <= 6) {
return weekdays[num];
} else {
return "Invalid input";
}
}
```
该函数接受一个整数参数 `num`,并返回对应的星期几名称。如果 `num` 不在 0-6 的范围内,则返回字符串 "Invalid input"。
使用示例:
```c
#include <stdio.h>
int main() {
int num = 3;
char* weekday = int_to_weekday(num);
printf("The day is %s\n", weekday);
return 0;
}
```
输出结果:
```
The day is Wednesday
```
相关问题
编写一个函数,将数字0-6分别转换为"Sunday","Monday","Tuesday","Wednsday","Thursday","Friday","Saturday"。char * int2week(int week)
好的,以下是函数的代码实现:
```c
char* int2week(int week) {
char* weekStr[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
if (week >= 0 && week <= 6) {
return weekStr[week];
} else {
return NULL;
}
}
```
这个函数接受一个数字参数 `week`,如果在 0 到 6 的范围内,就返回对应的星期字符串;否则返回 NULL。
c语言编写一个函数,将数字0-6分别转换为"Sunday","Monday","Tuesday","Wednsday","Thursday","Friday","Saturday"。char * int2week(int week)
下面是一个示例函数,可以将数字转换为对应的星期几名称:
```c
char* int2week(int week) {
static char* weeks[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
if (week >= 0 && week <= 6) {
return weeks[week];
} else {
return NULL;
}
}
```
该函数接受一个整数参数 `week`,并返回对应的星期几名称。如果 `week` 不在 0-6 的范围内,则返回 `NULL`。
使用示例:
```c
#include <stdio.h>
int main() {
int week = 3;
char* weekday = int2week(week);
if (weekday != NULL) {
printf("The day is %s\n", weekday);
} else {
printf("Invalid input\n");
}
return 0;
}
```
输出结果:
```
The day is Wednesday
```
阅读全文