/***************************************************************************
(c) Copyright 2002 Jeremy Russell, All Rights Reserved.
http://homepage.mac.com/hteric/Project4.pdf
****************************************************************************
File : cdate.c
Author : Jeremy Russell
Date : November 14, 2002
Description : Description should go here.
***************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 20
/* Funtion declaration. */
int convertOne2Two(char *);
int convertTwo2One(char *);
/* Global variable declaration. */
static char *months[] = {"Illegal Month", "January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
/***************************************************************************
Function :
Author : Jeremy Russell
Date : November 14, 2002
Description : 'main' function of cdate.c.
Arguments : void
Returns : void
Notes :
See Also :
***************************************************************************/
int main()
{
char date[SIZE];
printf("Please enter a date:\n");
gets(date);
if (isalpha(*date)) {
printf("This is in the second date format, the date converted to the first format is:\n");
if (convertTwo2One(date)) {
return 0;
}
printf("Conversion to format one failed.\n");
} else {
printf("This is in the first date format, the date converted to the second format is:\n");
if (convertOne2Two(date)) {
return 0;
}
printf("Conversion to format two failed.\n");
}
return 1;
}
/***************************************************************************
Function : convertOne2Two
Author : Jeremy Russell
Date : November 14, 2002
Description : Converts a date string of the format 'mm/dd/yyy' to a format
of 'Month dd, yyyy'.
Arguments : A date string of the format 'mm/dd/yyyy'.
Returns : int (true or false)
Notes :
See Also :
***************************************************************************/
int convertOne2Two(char * text)
{
char *tokens[4];
char delimiters[1] = {'/'};
int count = 0;
while (tokens[count] = strtok(text, delimiters)) {
text = NULL;
count++;
}
printf("%s %s, %s\n", months[atoi(tokens[0])], tokens[1], tokens[2]);
return 1;
}
/***************************************************************************
Function : convertTwo2One
Author : Jeremy Russell
Date : November 14, 2002
Description : Converts a date string of the format 'Month dd, yyyy' to a
format of 'mm/dd/yyyy'.
Arguments : A date string of the format 'Month dd, yyyy'.
Returns : int (true or false)
Notes :
See Also :
***************************************************************************/
int convertTwo2One(char * text)
{
char *tokens[4];
char delimiters[2] = {' ',','};
int monthInt = 0, count = 0;
while (tokens[count] = strtok(text, delimiters)) {
text = NULL;
count++;
}
for (count = 1; count < 13; count++) {
if (! strcmp(tokens[0], months[count])) {
monthInt = count;
break;
}
}
if (! monthInt) {
return 0;
}
printf("%02d/%02d/%4d\n", monthInt, atoi(tokens[1]), atoi(tokens[2]));
return 1;
}