Team Emertxe
Strings and Storage
Classes
Assignment 7
Assignment 7
Assignment 7
WAP to implement strtok function
Assignment 7
WAP to implement strtok function
Input:
Assignment 7
WAP to implement strtok function
Input: Read two Strings.
Assignment 7
WAP to implement strtok function
Input: Read two Strings.
Output:
Assignment 7
WAP to implement strtok function
Input: Read two Strings.
Output: Print tokens from string.
Assignment 7
What is strtok() function?
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.
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);
Assignment 7
Let’s understand how strtok() works:
Assignment 7
Let’s understand how strtok() works:
Assignment 7
Let’s understand how strtok() works:
What will be printed?
Assignment 7
Let’s understand how strtok() works:
“Are” will be printed
O/p of the program:
Are
Assignment 7
Let’s understand how strtok() works:
What will be printed?
Assignment 7
Let’s understand how strtok() works:
“Are” will be printed
“Are” will be printed
O/p of the program:
Are
Are
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
Assignment 7
Let’s understand how strtok() works:
What will be printed?
What will be printed?
Assignment 7
Let’s understand how strtok() works:
“you” will be printed
“Are” will be printed
O/p of the program:
Are
you
Assignment 7
Let’s understand how strtok() works:
O/p of the program:
Are
you
How will you print the
remaining tokens?
Assignment 7
Let’s understand how strtok() works:
What is the output?
Assignment 7
Let’s understand how strtok() works:
O/p of the program:
Are
You
okay
Assignment 7
How to implement your own strtok() function?
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
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
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
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
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
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
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
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’
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++
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
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
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
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
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
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
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
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
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’
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++
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
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
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
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
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
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
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
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
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
Assignment 7
Sample execution:-
Assignment 7
Sample execution:-
Assignment 7
Sample execution:-
Assignment 7
Pre-requisites:-
Assignment 7
Pre-requisites:-
⮚Strings
Assignment 7
Pre-requisites:-
⮚Strings
⮚Storage Classes
Assignment 7
Pre-requisites:-
⮚Strings
⮚Storage Classes
⮚Pointers
Assignment 7
Pre-requisites:-
⮚Strings
⮚Storage Classes
⮚Pointers
Objective:-
Assignment 7
Pre-requisites:-
⮚Strings
⮚Storage Classes
⮚Pointers
Objective:-
To understand the concept of String functions.
Team Emertxe
Thank you

07_strtok.pdf

  • 1.
    Team Emertxe Strings andStorage Classes
  • 2.
  • 3.
  • 4.
    Assignment 7 WAP toimplement strtok function
  • 5.
    Assignment 7 WAP toimplement strtok function Input:
  • 6.
    Assignment 7 WAP toimplement strtok function Input: Read two Strings.
  • 7.
    Assignment 7 WAP toimplement strtok function Input: Read two Strings. Output:
  • 8.
    Assignment 7 WAP toimplement strtok function Input: Read two Strings. Output: Print tokens from string.
  • 9.
    Assignment 7 What isstrtok() function?
  • 10.
    Assignment 7 What isstrtok() 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 isstrtok() 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);
  • 12.
  • 13.
  • 14.
    Assignment 7 Let’s understandhow strtok() works: What will be printed?
  • 15.
    Assignment 7 Let’s understandhow strtok() works: “Are” will be printed O/p of the program: Are
  • 16.
    Assignment 7 Let’s understandhow strtok() works: What will be printed?
  • 17.
    Assignment 7 Let’s understandhow strtok() works: “Are” will be printed “Are” will be printed O/p of the program: Are Are
  • 18.
    Assignment 7 Let’s understandhow 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
  • 19.
    Assignment 7 Let’s understandhow strtok() works: What will be printed? What will be printed?
  • 20.
    Assignment 7 Let’s understandhow strtok() works: “you” will be printed “Are” will be printed O/p of the program: Are you
  • 21.
    Assignment 7 Let’s understandhow strtok() works: O/p of the program: Are you How will you print the remaining tokens?
  • 22.
    Assignment 7 Let’s understandhow strtok() works: What is the output?
  • 23.
    Assignment 7 Let’s understandhow strtok() works: O/p of the program: Are You okay
  • 24.
    Assignment 7 How toimplement your own strtok() function?
  • 25.
    Assignment 7 How toimplement 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 re ; 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 re ; 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 re ; 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 re ; 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 re ; 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 re ; 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 re ; 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 re 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 re 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 re 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 re 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 re 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 re 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 re 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 re 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 re 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 re 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 re 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 re 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 re 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 re 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 re 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 re 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 re 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 re 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 re 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 re 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
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.