convert 03 May 2012 format to 03-05-2012

I saved 3 May 2012 inside a text file and i want to convert it to 03-05-2012 format.. so far i can only think of structure and thinking about function....
struct Date{
int date;
char month;
int year;
};

please can some1 help me?
You would have to take the string and parse it getting the day, and if it is below 10, add an '0' and then take the Month and use a switch to convert it to a number and then the year you have.
thank you i got the roughly meaning of what you mean but i can't figure out how to break down 03 may 2012 into date month year format after extracting out from text file.
You could use boost.date_time library I/O (works on any compiler) or C++11 I/O (works on new compilers), if you're willing to use this opportunity to learn how to work with dates in C++.

boost:
http://www.boost.org/doc/libs/release/doc/html/date_time/date_time_io.html

C++:
http://en.cppreference.com/w/cpp/io/manip/get_time
http://en.cppreference.com/w/cpp/io/manip/put_time

(personally, I think boost approach is much better)
Last edited on
The main idea is to:
1. Get a line of text like: "03 May 2012"
2. Create three variables, a int, a char* and another int
3. Get the first int
4. Get the Text representing the month
5. Get the last int
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <cctype> // tolower
char Text[] = "03 May 2012";
char Temp[256] = {0};
const char * p = Text;
int Day = 0;
int Month = 0;
int Year = 0;
unsigned int Length = strlen(p);
bool strccmp(const char * First, const char * Second) // Like strcmp, but will work with case-different words
{
    unsigned int i = 0;
    while(1)
    {
        if(tolower(First[i]) != tolower(Second[i]))
            return 1;
        if(First[i] <= 0)
            break;
        ++i;
    }
    return 0;
}
for(unsigned int i = 0; i < Length; ++i)
{
    Temp[i] = p[i]; // Copy until you find a space
    if(Temp[i] == ' ') // If you 'meet' a space, remove it and stop
    {
        p += i+1; // Next time continue parsing from here
        Temp[i] = 0; break;
    }
}
Day = _atoi(Temp); // Here, you've got the day!
unsigned int Length = strlen(p);
for(unsigned int i = 0; i < Length; ++i)
{
    Temp[i] = p[i]; // Copy until you find a space
    if(Temp[i] == ' ') // If you 'meet' a space, remove it and stop
    {
        p += i+1; // Next time continue parsing from here
        Temp[i] = 0; break;
    }
}
Month = 1;
do {
    if(!strccmp(Temp,"January"))
        break;
    ++Month;
    if(!strccmp(Temp,"February"))
        break;
    ++Month;
    if(!strccmp(Temp,"March"))
        break;
    ++Month;
    if(!strccmp(Temp,"April"))
        break;
    ++Month;
    if(!strccmp(Temp,"May"))
        break;
    ++Month;
    if(!strccmp(Temp,"June"))
        break;
    ++Month;
    if(!strccmp(Temp,"July"))
        break;
    ++Month;
    if(!strccmp(Temp,"August"))
        break;
    ++Month;
    if(!strccmp(Temp,"September"))
        break;
    ++Month;
    if(!strccmp(Temp,"October"))
        break;
    ++Month;
    if(!strccmp(Temp,"November"))
        break;
    ++Month;
    if(!strccmp(Temp,"December"))
        break;
    ++Month;
} while(0);
if(Month == 13) { /* Error, invalid month */ }
unsigned int Length = strlen(p);
for(unsigned int i = 0; i < Length; ++i)
{
    Temp[i] = p[i]; // Copy until you find a space
    if(Temp[i] == ' ') // If you 'meet' a space, remove it and stop
    {
        p += i+1; // Next time continue parsing from here
        Temp[i] = 0; break;
    }
}
Year = _atoi(Temp); // Here, you've got the Year! 


This is the "way" to do it. It will only gather the integer. You should complete it.
Last edited on
Thank you for your help.. i'm still trying.. i got the idea from your reply but i think strcpy will be more easier for me now. i need to read more books. :)
No, sorry, what I'm doing is NOT a strcpy. What i did will copy the text until a space is found. Strcpy will copy a entire string - also will copy spaces.
So if you extend it to the Month and Year, you will be fully-abled to use it. Want me to complete it? I'm gonna edit that post, check it out within minutes.

EDIT: Finished. Will recognize strings like this:

25 May 2012
5 OctOBeR 1922
1922 jANUARY 5 (1922 will still be the day, and 5 will be the year!!)
Last edited on
Topic archived. No new replies allowed.