【发布时间】:2021-12-05 08:34:05
【问题描述】:
这是任务: 1)07/21/2003 2)2003 年 7 月 21 日
编写一个程序,以第一种格式读取日期并以第二种格式打印。
我应该使用字符串方法,尤其是 strtok。
这是我的代码:
#include <stdio.h>
#include <string.h>
int main()
{
char date[20];
char month[20];
char day[20];
char year[20];
char *token;
char sep[1] = "/";
printf("Enter the date (MM/DD/YYYY): ");
gets(date);
token = strtok(date, sep);
if (token == "01")
{
strcpy(month, "Januray");
}
else if (token == "02")
{
strcpy(month, "February");
}
//continuing in this way
}
else if (token == "11")
{
strcpy(month, "November");
}
else
{
strcpy(month, "December");
}
token = strtok(NULL, sep);
strcpy(day, token);
token = strtok(NULL, sep);
strcpy(year, token);
printf("%s %s, %s", month, day, year);
}
问题是月份部分总是给出十二月,这意味着如果语句不起作用。
【问题讨论】:
-
该令牌的价值究竟是什么是?
-
在 C 中(请标记)我认为您不能将字符串值与 == 进行比较
-
“我应该使用字符串方法”。其中之一是
strcmp,检查它的作用。 -
@HansKesting 和字符串一样,我用strcmp方法测试它返回值0
-
到底检查了什么?在哪里? “我将双方程改为单方程”这不是你应该做的。再次检查
strcmp的作用。
标签: c if-statement strtok