[C/C++基礎]-純C實作字串切割(拆解)split函數
由於最近要利用JAVA來實作字串切割(拆解),所以順便寫一份純C實作字串切割範例,歡迎有興趣的同好也來C/P一下。
#include <stdio.h> #include <stdlib.h> #include <string.h> void split(char **arr, char *str, const char *del) { char *s = strtok(str, del); while(s != NULL) { *arr++ = s; s = strtok(NULL, del); } } int main () { char str[] ="10,20,30"; const char *del = ","; char *arr[3]; split(arr, str, del); int i=0; while(i<3) printf("%s\n", *(arr+i++));
return 0; }
|
沒有留言:
張貼留言