Hi,


I am new member in this location.

I wanted to quaering about format the date in the computer system .


in our class we study C ++ language.
and learned this build in function :
strtok ( , )
that take the variable in first argument , until see the character in another argument.

so, we used this function in cutting the date in the system 3 times.
strtok (variable, "/") ;
because we used storing the day , month and year in different variables.
but my quaering .
Why using "/" in the third time.?
and I searched about format the date in the computer system to see how it writing, may be it written in month/ day/ year/ time.
but it written in format:
date: month/ day/ year
time :...

What do you think the reason ?
I hope to find the reason in this location.


my greeting
thank you

strtok() will try to find whichever characters you'll pass it as the second parameter. If you give it a '/', it will return a pointer to the first time it encounters that character. Enclose it into a proper designed loop and you can use it to chop the given array of characters into tokens, where the '/' is the separator.
A little drawback is the given array string has to be able to accept reassignment, since in order to keep track of where it encounters the given character a NULL is reassigned to it.

thanks a lot for your help.

according to what i understand,
if i take the date of the computer system i chop then store it into three array of string. B.coz we encounter 3 separator > in this format (month /day /year / ) ,
and when i finish the first storage 'month' and want to start a new one 'day' i start from a NULL.

right?


MY Q
if i use the function for the third time, why i should put / in the second argument.? if so, the 'date format' will be 'month/day/year/' while we usually don't put it in writing the normal date format > 'month/ day/ year'??!!


accept my deep regards
mini

If the string only contains a date (month/day/year) then you're right the third '/' isn't strictly necessary, but you do need to call it a third time to get the year and the '/' works about as well as anything else that would not be in the 'year' part.

If the string contained a date and a time like (month/day/year hour:minute:second) then the third call would use a ' ', the fourth a ':', the fifth a ':' and the last anything not in the second, but a ':' would work.

but you do need to call it a third time to get the year , and the '/' works about as well as anything

you mean I can replace "/" with any other character .?
e.g: " " or "," .

or I must use the final character used?

Please, don't worry about that sacramental third call!
If strtok can't find third slash it returns the pointer to the start of year field (or what else after the second slash), but the next call returns NULL.
In other words, strtok returns NULL only when all c-string was scanned. For example, if you have "19/12/" or "19/12" string w/o year field then the third call returns NULL.
Of course, you can't <replace "/" with any other character>: for example, you get a very strange year from "19/12/2008" if you call strtok(0,"0") ;)

I think I went blind trying to read all that red and green text on white.

I think I went blind trying to read all that red and green text on white.

Completely subscribed to an opinion.

ArkM

thank you ever sooooooo much;

Please, don't worry about that sacramental third call!

HaHaHa

i think i should be worried because it is apart of my research. ^_^


i have another question >>> ^_*

is it a must to use the character"/" in the third time B.coz it's the last one used (in the second time?) i.e is it important to make the last two characters the same?
============

strtok returns NULL only when all c-string was scanned. For example,if you have "19/12/" or "19/12" string w/o year field then the third call returns NULL.

could you please explain this part more>> i'd like to "settle it well in my head"

thanx sooooo much
and I am sorry !!!
i think these Qs make your life difficult >> ^_*

None of the characters ever "HAVE" to match anything.

The way it works is something like this:

Lets say we have a char * date that points to "01/12/2009" which is in memory as '0','1','/','1','2','/','2','0','0','9','\0' when you call strtok(date, "/") it actually returns a pointer to the first character and modifies the memory to look like this: '0','1','\0','1','2','/','2','0','0','9','\0' note that it changed the first '/' into a '\0'.

When you call strtok(NULL, "/") it remembers the last place that it changed and starts searching from there. So it steps to the next character (following the one it changed to '\0') and compares it to the delimiter '/' in this case. '1' isn't '/' so it steps again and keeps looking. '2' isn't '/' either so it steps again. The next chararcter '/' is the delimiter, so it changes it to a '\0' and returns the a pointer to the first character it started with (the '1' in this case). Now the memory looks like: '0','1','\0','1','2','\0','2','0','0','9','\0' note that the original 'date' still points to the first '0' in that memory string.

Now for the (sarcasm on) all important third call and its delimiter (sarcasm off) when you call strtok(NULL, '/') it does what it did in the last call, but it never finds the '/' so it just returns a pointer to the character following the character it last changed (the '2' in the year). The memory still looks like '0','1','\0','1','2','\0','2','0','0','9','\0' but the code knows that it didn't find the delimiter so it stops remembering where it left off so that if you call strtok(NULL, "/") (or with any delimiter) again, it will return NULL indicating that there was no more to find.

If you had changed the delimiters to the calls, you would have gotten diffrerent results. Here's a shortened version of what happens with a different order of calls:

char * date = "01/12/2009";
char * tmp = strtok(date, "/");
// tmp points to "01" from the start of the string
tmp = strtok(date, "0");
// tmp points to "12/2" from the month/year.
tmp = strtok(date, "@");
// tmp points to the "09" from the end of the year.

Side Note: if this is part of your research, you should have been conducting experiments. That's how I determined how this all worked for myself. I wanted to understand it so I wrote a series of tests, ran them and examined the results. Its not that hard.

commented: Well explained. Thanks. +1

Lets say we have a char * date that points to "01/12/2009" which is in memory as '0','1','/','1','2','/','2','0','0','9','\0' when you call strtok(date, "/") it actually returns a pointer to the first character and modifies the memory to look like this: '0','1','\0','1','2','/','2','0','0','9','\0' note that it changed the first '/' into a '\0'.

char * date = "01/12/2009";
char * tmp = strtok(date, "/");
// tmp points to "01" from the start of the string
tmp = strtok(date, "0");
// tmp points to "12/2" from the month/year.
tmp = strtok(date, "@");
// tmp points to the "09" from the end of the year.

Incorrect. char *date is read only memory, therefore strtok() can not work with it. Notice my previous statement.

A little drawback is the given array string has to be able to accept reassignment, since in order to keep track of where it encounters the given character a NULL is reassigned to it.

Change char *date to char date[]

>...of my research...
It's not terra incognita, it's well-defined the C programming language ;)
The C Standard (7.21.5.8):

2 A sequence of calls to the strtok function breaks the string pointed to by s1 into a sequence of tokens, each of which is delimited by a character from the string pointed to by s2. The first call in the sequence has a non-null first argument; subsequent calls in the sequence have a null first argument. The separator string pointed to by s2 may be different from call to call.
3 The first call in the sequence searches the string pointed to by s1 for the first character that is not contained in the current separator string pointed to by s2. If no such character is found, then there are no tokens in the string pointed to by s1 and the strtok function returns a null pointer. If such a character is found, it is the start of the first token.
4 The strtok function then searches from there for a character that is contained in the current separator string. If no such character is found, the current token extends to the end of the string pointed to by s1, and subsequent searches for a token will return a null pointer. If such a character is found, it is overwritten by a null character, which terminates the current token. The strtok function saves a pointer to the following character, from which the next search for a token will start.
5 Each subsequent call, with a null pointer as the value of the first argument, starts searching from the saved pointer and behaves as described above.
6 The implementation shall behave as if no library function calls the strtok function.
Returns
7 The strtok function returns a pointer to the first character of a token, or a null pointer if there is no token.
8 EXAMPLE

#include <string.h>
static char str[] = "?a???b,,,#c";
char *t;
t = strtok(str, "?"); // t points to the token "a"
t = strtok(NULL, ","); // t points to the token "??b"
t = strtok(NULL, "#,"); // t points to the token "c"
t = strtok(NULL, "?"); // t is a null pointer

I value your note.
I learned this language from one 1 course before now.
and I love it, so I always conduct experiments .

but now , My teacher requested us to bring a clear reason for the (third call '/' ) 2 weeks ago , from that time I didn't stop expermints but i felt that a hand from the web will be fine.

sorry for wasting your time

and please accept my excuse!!
-- sorry again --

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.