This is in rogramming language C Write a program that takes as input four dates
ID: 3847150 • Letter: T
Question
This is in rogramming language C
Write a program that takes as input four dates in the format MM:dd:yyyy and prints them out in a tabular format, as shown below (items underlined are to be entered by the user): The year, month and day columns should be separated by tabs ( ) (this also refers to the words Year, Month and Day from the header of the table). The year column should occupy 4 spaces and the values should be right justified. The month column should occupy 2 spaces and the values should be right justified. The day column should occupy 2 spaces and the values should be left justified. Save your program in a file called date.c. Modify your previous program so that if the value of the year is less than 4 digits, add 0 in front of the year to make the year has 4 digits. If the values of month and day are less than 10 (meaning they have just one digit), the values of month/day will always have two digits displayed (as shown below): Save your program in a file called date c.c.Explanation / Answer
//Problem1:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 100
int main()
{
char date_str[12];
char str[5];
char c1, c2, c3;
struct date
{
char year[5];
char month[3];
char day[3];
} Date[MAX];
int count = 0,i,j =0,k = 0 ;
//take user input till user enters 0 to quit
do
{
printf("Enter date %d (MM/dd/yyyy) : ", count+1);
scanf("%s", date_str);
if (strcmp(date_str, "0") == 0)
break;
i = 0;
k = 0;
j = 0;
while (date_str[i] != '')
{
if (date_str[i] != '/')
str[j++] = date_str[i++];
else
{
switch (k)
{
case 0:
str[j] = '';
strcpy(Date[count].month ,str);
i++;
j = 0;
k++;
break;
case 1:
str[j] = '';
strcpy(Date[count].day, str);
i++;
j = 0;
k++;
break;
}
}
}
str[j] = '';
strcpy(Date[count].year, str);
count++;
} while (strcmp(date_str, "0") != 0);
//now display the date in tabular form
printf("---------------------------------------------------------------------------- ");
printf("Year Month Day ");
for (i = 0; i < count; i++)
{
printf("%4s %2s %2s ", Date[i].year, Date[i].month, Date[i].day);
}
}
---------------------------------------------
//output problem1
Enter date 1 (MM/dd/yyyy) : 5/3/2017
Enter date 2 (MM/dd/yyyy) : 2/13/2010
Enter date 3 (MM/dd/yyyy) : 7/4/1776
Enter date 4 (MM/dd/yyyy) : 11/13/200
Enter date 5 (MM/dd/yyyy) : 0
----------------------------------------------------------------------------
Year Month Day
2017 5 3
2010 2 13
1776 7 4
200 11 13
-------------------------------
//problem2
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 100
int main()
{
char date_str[12],str1[5],str2[3],str3[3];
char str[5];
char c1, c2, c3;
struct date
{
char year[5];
char month[3];
char day[3];
} Date[MAX];
int count = 0,i,j =0,k = 0 ;
//take user input till user enters 0 to quit
do
{
printf("Enter date %d (MM/dd/yyyy) : ", count+1);
scanf("%s", date_str);
if (strcmp(date_str, "0") == 0)
break;
i = 0;
k = 0;
j = 0;
while (date_str[i] != '')
{
if (date_str[i] != '/')
str[j++] = date_str[i++];
else
{
switch (k)
{
case 0:
str[j] = '';
strcpy(Date[count].month ,str);
i++;
j = 0;
k++;
break;
case 1:
str[j] = '';
strcpy(Date[count].day, str);
i++;
j = 0;
k++;
break;
}
}
}
str[j] = '';
strcpy(Date[count].year, str);
count++;
} while (strcmp(date_str, "0") != 0);
//now display the date in tabular form
printf("---------------------------------------------------------------------------- ");
printf("Year Month Day ");
for (i = 0; i < count; i++)
{
strcpy(str1, Date[i].year);
strcpy(str2, Date[i].month);
strcpy(str3, Date[i].day);
if (strlen(Date[i].year) < 4)
{
strcpy(str1, "0");
strcat(str1, Date[i].year);
}
if (strlen(Date[i].month) < 2)
{
strcpy(str2, "0");
strcat(str2, Date[i].month);
}
if (strlen(Date[i].day) < 2)
{
strcpy(str3, "0");
strcat(str3, Date[i].day);
}
printf("%4s %2s %2s ", str1, str2, str3);
}
}
-----------------------------------------------------------
//output problem2
Enter date 1 (MM/dd/yyyy) : 5/3/2017
Enter date 2 (MM/dd/yyyy) : 2/13/2010
Enter date 3 (MM/dd/yyyy) : 7/4/1776
Enter date 4 (MM/dd/yyyy) : 11/13/200
Enter date 5 (MM/dd/yyyy) : 0
-----------------------------------------------------------------------
Year Month Day
2017 05 03
2010 02 13
1776 07 04
0200 11 13
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.