#include <stdio.h><br />#include <string.h><br />/* 以字元ch為界分離字串s2為前後兩部分 */<br />char *_string_parse(char *s1, char *s2, char ch)<br />{<br />while(*s2 == ch) s2++;<br />while(*s2) {<br />if (*s2 == ch) {<br />while (*++s2 == ch);<br />break;<br />}<br />*s1++ = *s2++;<br />}<br />*s1 = '/0';<br />return s2;<br />}<br />/* 按字元*ch將字串s2分割成一個數組字串 */<br />char **string_parse(char *s2, char *ch)<br />{<br />char **ccp;<br />char *s1;<br />char *cp = s2;<br />int i = 0;<br />while (strcmp(cp, "")) {<br />cp = _string_parse(s1, cp, *ch);<br />ccp[i] = strdup(s1);<br />i++;<br />}<br />ccp[i] = strdup("");<br />return ccp;<br />}<br />/* 測試 */<br />int main()<br />{<br />char *ipc = "<<<<<<<abc<edfg<hijklmn";<br />char *cp = ipc;<br />char *p;<br />char str[100];<br />char *p2, *p3;<br />char **aa;<br />char *s1;<br />int i = 0;<br />char ch = '<';</p><p>aa = string_parse(ipc, &ch);<br />for (; strcmp(aa[i], "") != 0; ++i) {<br />printf("array string %s/n", aa[i]);<br />}<br />printf("%s/n", aa[0]);<br />/* 還有點問題:1,程式現在還不能按字串來分割字串。2,程式還有個警告:Null pointer assignment */<br />return 0;<br />}<br />