Reputation: 33
I'm trying to construct a program that has the user input the date in this format '01'14'2013', and outputs it into this format 'january 14, 2013'. I am trying to copy the string that holds the input from the user onto a different string, to later concatenate it onto the original string without the first and second index of the strings, so that I only have '/14/2013', from the original string, and then replace the '/' with ' ' so that it reads the month, day and the year....but for some reason, when I try to copy the original string from input onto another string( the one I plan to concatenate later), it doesn't copy effectively, am i missing something..?
#include <stdio.h>
#include <string.h>
int main()
{
char date[100];
char month[100];
char array[12][100] ={"January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
char month2[100];
printf(" Please enter a date ");
fgets( date, 100, stdin);
strcpy(month2, month);
if( date[0] == '0' && date[1] == '1')
{
strcpy(month, array[0]);
}
else if( date[0] =='0' && date[1] == '2')
{
strcpy(month, array[1]);
}
else if( date[0] =='0' && date[1] == '3')
{
strcpy(month, array[2]);
}
else if( date[0] =='0' && date[1] == '4')
{
strcpy(month, array[3]);
}
else if( date[0] =='0' && date[1] == '5')
{
strcpy(month, array[4]);
}
else if( date[0] == '0' && date[1] == '6')
{
strcpy(month, array[5]);
}
else if( date[0] =='0' && date[1] == '7')
{
strcpy(month, array[6]);
}
else if( date[0] =='0' && date[1] == '8')
{
strcpy(month, array[7]);
}
else if( date[0] =='0' && date[1] == '9')
{
strcpy(month, array[8]);
}
else if( date[0] =='1' && date[1] == '0')
{
strcpy(month, array[9]);
}
else if( date[0] =='1' && date[1] == '1')
{
strcpy(month, array[10]);
}
else if( date[0] =='1' && date[1] == '2')
{
strcpy(month, array[11]);
}
printf("%s \n", month);
printf("%s \n", month2);
return 0;
}
Upvotes: 0
Views: 872
Reputation: 40145
#include <stdio.h>
#include <string.h>
#include <ctype.h>
typedef struct mon {
const char *name;
const int len;
} Month;
#define M(x){ x " ", sizeof(x)}
Month month[] = { {"", 0}, //dummy
M("January"), M("Febuary"), M("March"), M("April"),
M("May"), M("June"), M("July"), M("August"),
M("September"), M("October"), M("November"), M("December")
};
int main(){
char in_date[128];
char out_date[128] = "";
int m = 0, pos;
printf(" Please enter a date E.g MM/DD/YYYY\n");
fgets( in_date, sizeof(in_date), stdin);
if(in_date[0] == '0'){
if(isdigit(in_date[1]) && in_date[1] != '0'){
m = in_date[1] - '0';
pos = month[m].len;
memcpy(out_date, month[m].name, pos);
}
} else if(in_date[0] == '1'){
if('0' <= in_date[1] && in_date[1] <= '2'){
m = 10 + in_date[1] - '0';
pos = month[m].len;
memcpy(out_date, month[m].name, pos);
}
}
if(m){
memcpy(out_date + pos, in_date + 3, 7);
out_date[pos + 2] = ',';
out_date[pos + 7] = '\0';
printf("%s\n", out_date);
} else {
printf("invalid month\n");
}
return 0;
}
Upvotes: 0
Reputation: 124642
strcpy(month2, month);
Neither month
nor month2
have been initialized to anything useful at this point. Their contents are indeterminate and calling strcpy
with something other than a properly terminated C-string invokes undefined behavior.
Looks like a typo to me.
Upvotes: 1
Reputation: 1311
Lesser code, But without some validations.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char date[100];
char month[100];
char array[12][100] = {"January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
printf(" Please enter a date ");
fgets( date, 100, stdin);
char month2[100];
strcpy(month2, date);
month2[2] = '\0';
strcpy(month, array[atoi(month2) - 1]);
printf("%s \n", month);
return 0;
}
Upvotes: 0