10. Assignment 7
What is strtok() function?
The strtok() function breaks a string into a sequence of zero or
more nonempty tokens.
On the first call to strtok(), the string to be parsed should be
specified in str. In each subsequent call that should parse the
same string, str must be NULL.
11. Assignment 7
What is strtok() function?
The strtok() function breaks a string into a sequence of zero or
more nonempty tokens.
On the first call to strtok(), the string to be parsed should be
specified in str. In each subsequent call that should parse the
same string, str must be NULL.
Prototype: char *strtok(char *str, const char *delim);
17. Assignment 7
Let’s understand how strtok() works:
“Are” will be printed
“Are” will be printed
O/p of the program:
Are
Are
18. Assignment 7
Let’s understand how strtok() works:
But, the next token
should be printed is
“you”
“Are” will be printed
“Are” will be printed
O/p of the program:
Are
Are
25. Assignment 7
How to implement your own strtok() function?
Input: str = “Are;you:okay”
delim = “;:”
A r e ; y o u : o k a y 0
; : 0
str
delim String containing delimiters,
these delimiters has to be
matched with each and every
character of str
26. Assignment 7
A r e ; y o u : o k a y 0
; : 0
str
delim
0 1 2 3 4 5 6 7 8 9 10 11 12
start_index
Input: str = “Are;you:okay”
delim = “;:”
pos
27. Assignment 7
A r e ; y o u : o k a y 0
; : 0
str
delim
0 1 2 3 4 5 6 7 8 9 10 11 12
start_index
Input: str = “Are;you:okay”
delim = “;:”
pos
28. Assignment 7
A r e ; y o u : o k a y 0
; : 0
str
delim
0 1 2 3 4 5 6 7 8 9 10 11 12
start_index
Input: str = “Are;you:okay”
delim = “;:”
pos
29. Assignment 7
A r e ; y o u : o k a y 0
; : 0
str
delim
0 1 2 3 4 5 6 7 8 9 10 11 12
start_index
Input: str = “Are;you:okay”
delim = “;:”
pos
30. Assignment 7
A r e ; y o u : o k a y 0
; : 0
str
delim
0 1 2 3 4 5 6 7 8 9 10 11 12
start_index
Input: str = “Are;you:okay”
delim = “;:”
pos
31. Assignment 7
A r e ; y o u : o k a y 0
; : 0
str
delim
0 1 2 3 4 5 6 7 8 9 10 11 12
start_index
Input: str = “Are;you:okay”
delim = “;:”
pos
32. Assignment 7
A r e ; y o u : o k a y 0
; : 0
str
delim
0 1 2 3 4 5 6 7 8 9 10 11 12
start_index
Input: str = “Are;you:okay”
delim = “;:”
pos Here at pos 3, delimiter is
encountered, so overwrite that
byte with ‘0’
33. Assignment 7
A r e 0 y o u : o k a y 0
; : 0
str
delim
0 1 2 3 4 5 6 7 8 9 10 11 12
start_index
Input: str = “Are;you:okay”
delim = “;:”
pos Return the address using str +
start_index and do pos++
34. Assignment 7
A r e 0 y o u : o k a y 0
; : 0
str
delim
0 1 2 3 4 5 6 7 8 9 10 11 12
start_index
Input: str = “Are;you:okay”
delim = “;:”
pos Return the address using str +
start_index and do pos++
Output:
Are
35. Assignment 7
A r e 0 y o u : o k a y 0
; : 0
str
delim
0 1 2 3 4 5 6 7 8 9 10 11 12
start_index
Input: str = “Are;you:okay”
delim = “;:”
pos
Output:
Are
36. Assignment 7
A r e 0 y o u : o k a y 0
; : 0
str
delim
0 1 2 3 4 5 6 7 8 9 10 11 12
start_index
Input: str = “Are;you:okay”
delim = “;:”
pos
Output:
Are
37. Assignment 7
A r e 0 y o u : o k a y 0
; : 0
str
delim
0 1 2 3 4 5 6 7 8 9 10 11 12
start_index
Input: str = “Are;you:okay”
delim = “;:”
pos
Output:
Are
38. Assignment 7
A r e 0 y o u : o k a y 0
; : 0
str
delim
0 1 2 3 4 5 6 7 8 9 10 11 12
start_index
Input: str = “Are;you:okay”
delim = “;:”
pos
Output:
Are
39. Assignment 7
A r e 0 y o u : o k a y 0
; : 0
str
delim
0 1 2 3 4 5 6 7 8 9 10 11 12
start_index
Input: str = “Are;you:okay”
delim = “;:”
pos
Output:
Are
40. Assignment 7
A r e 0 y o u : o k a y 0
; : 0
str
delim
0 1 2 3 4 5 6 7 8 9 10 11 12
start_index
Input: str = “Are;you:okay”
delim = “;:”
pos
Output:
Are
41. Assignment 7
A r e 0 y o u : o k a y 0
; : 0
str
delim
0 1 2 3 4 5 6 7 8 9 10 11 12
start_index
Input: str = “Are;you:okay”
delim = “;:”
pos
Output:
Are
42. Assignment 7
A r e 0 y o u : o k a y 0
; : 0
str
delim
0 1 2 3 4 5 6 7 8 9 10 11 12
start_index
Input: str = “Are;you:okay”
delim = “;:”
pos
Output:
Are
Here at pos 7, delimiter is
encountered, so overwrite that
byte with ‘0’
43. Assignment 7
A r e 0 y o u 0 o k a y 0
; : 0
str
delim
0 1 2 3 4 5 6 7 8 9 10 11 12
start_index
Input: str = “Are;you:okay”
delim = “;:”
pos
Output:
Are
You
Return the address using str +
start_index and do pos++
44. Assignment 7
A r e 0 y o u 0 o k a y 0
; : 0
str
delim
0 1 2 3 4 5 6 7 8 9 10 11 12
start_index
Input: str = “Are;you:okay”
delim = “;:”
pos
Output:
Are
You
45. Assignment 7
A r e 0 y o u 0 o k a y 0
; : 0
str
delim
0 1 2 3 4 5 6 7 8 9 10 11 12
start_index
Input: str = “Are;you:okay”
delim = “;:”
pos
Output:
Are
You
46. Assignment 7
A r e 0 y o u 0 o k a y 0
; : 0
str
delim
0 1 2 3 4 5 6 7 8 9 10 11 12
start_index
Input: str = “Are;you:okay”
delim = “;:”
pos
Output:
Are
You
47. Assignment 7
A r e 0 y o u 0 o k a y 0
; : 0
str
delim
0 1 2 3 4 5 6 7 8 9 10 11 12
start_index
Input: str = “Are;you:okay”
delim = “;:”
pos
Output:
Are
You
48. Assignment 7
A r e 0 y o u 0 o k a y 0
; : 0
str
delim
0 1 2 3 4 5 6 7 8 9 10 11 12
start_index
Input: str = “Are;you:okay”
delim = “;:”
pos
Output:
Are
You
49. Assignment 7
A r e 0 y o u 0 o k a y 0
; : 0
str
delim
0 1 2 3 4 5 6 7 8 9 10 11 12
start_index
Input: str = “Are;you:okay”
delim = “;:”
pos
Output:
Are
You
50. Assignment 7
A r e 0 y o u 0 o k a y 0
; : 0
str
delim
0 1 2 3 4 5 6 7 8 9 10 11 12
start_index
Input: str = “Are;you:okay”
delim = “;:”
pos
Output:
Are
You
51. Assignment 7
A r e 0 y o u 0 o k a y 0
; : 0
str
delim
0 1 2 3 4 5 6 7 8 9 10 11 12
start_index
Input: str = “Are;you:okay”
delim = “;:”
pos
Output:
Are
You
52. Assignment 7
A r e 0 y o u 0 o k a y 0
; : 0
str
delim
0 1 2 3 4 5 6 7 8 9 10 11 12
start_index
Input: str = “Are;you:okay”
delim = “;:”
pos
Output:
Are
You
okay
Return the address using str +
start_index