0
我試圖將以dd/mm/yyyy格式輸入的日期更改爲月份日,年格式,並且我已經完成了大部分操作,但是我的輸出在添加了額外的奇怪字符之後那天。這是我的代碼C中的日期格式
#include <stdio.h>
#include <string.h>
void main()
{
char userDate[11];
char dateWord[11];
char day[2];
char year[4];
printf("Welcome to the Date Formatter.\nPlease input a date in the form of mm/dd/yyyy.\n");
scanf("%s", userDate);
day[0] = userDate[3];
day[1] = userDate[4];
year[0] = userDate[6];
year[1] = userDate[7];
year[2] = userDate[8];
year[3] = userDate[9];
if (userDate[0] == '0' && userDate[1] == '1')
strcpy(dateWord, "January");
if (userDate[0] == '0' && userDate[1] == '2')
strcpy(dateWord, "February");
if (userDate[0] == '0' && userDate[1] == '3')
strcpy(dateWord, "March");
if (userDate[0] == '0' && userDate[1] == '4')
strcpy(dateWord, "April");
if (userDate[0] == '0' && userDate[1] == '5')
strcpy(dateWord, "May");
if (userDate[0] == '0' && userDate[1] == '6')
strcpy(dateWord, "June");
if (userDate[0] == '0' && userDate[1] == '7')
strcpy(dateWord, "July");
if (userDate[0] == '0' && userDate[1] == '8')
strcpy(dateWord, "August");
if (userDate[0] == '0' && userDate[1] == '9')
strcpy(dateWord, "September");
if (userDate[0] == '1' && userDate[1] == '0')
strcpy(dateWord, "October");
if (userDate[0] == '1' && userDate[1] == '1')
strcpy(dateWord, "November");
if (userDate[0] == '1' && userDate[1] == '2')
strcpy(dateWord, "December");
printf("The date is:\n");
printf("%s %s, %s\n", dateWord, day, year);
}
,輸出是
c:\CompSci\C Programming>dateconvert
Welcome to the Date Formatter.
Please input a date in the form of mm/dd/yyyy.
01/23/1998
The date is:
January 23╢ , 1998
我不知道正在打印的╢是爲什麼。
'char day [2];' - >'char day [3];'then'day [2] ='\ 0''。否則,你在'天'中沒有有效的字符串。 'year'同樣的問題。 – kaylum
哇,我覺得那愚蠢的..非常感謝你! – jpantalion
習慣這種感覺 - 它不會是最後一次':)' –