C按字元分割字串

來源:互聯網
上載者:User

#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 />

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.