요약:

C스타일로 문자열을 토큰화 하는 방법이다.

Features:
. 원본 문자열에 손상이 일어나므로 주의해야 한다.
. char *str = "..." 형식으로 사용하면 안된다.
    Program 영역에 있는 문자열을 사용하게 되므로 Segmentation fault가 일어난다.
. 첫번째 인자에 NULL이 들어가면 '계속하기'라는 의미이다.


예제:

http://codepad.org/MfMc40qx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* strtok example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="- This, a sample string.";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ,.-");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.-");
  }
  return 0; 
} 


Output:
1
2
3
4
5
Splitting string "- This, a sample string." into tokens:
This
a
sample
string



+ Recent posts