|
发表于 2020-6-3 02:20:25
|
显示全部楼层
本楼为最佳答案
本帖最后由 Darth_EF 于 2020-6-3 02:21 编辑
- #include <stdio.h>
- #include <stdlib.h>
- const int arrMax = 1024;
- typedef struct ChatXX_length{
- char** paths;
- int length;
- }ChatXX_length;
- void printChatXX(ChatXX_length data){
- int i = 0;
- char c[arrMax];
- for(;i<data.length;++i){
- printf("%s\n", data.paths[i]);
- }
- }
- ChatXX_length split(const char *data, const char *_flag);
- int main()
- {
- char d[]="ip route add 192.168.3.10 0x69 ip route add 192.168.3.20 0x69 ip route add 192.168.3.10",
- f[]="0x69";
- ChatXX_length c=split(d, f);
- printChatXX(c);
- return 0;
- }
- ChatXX_length split(const char *data, const char *_flag)
- {
- ChatXX_length returnValue;
- char** paths;
- int i = 0, j = 0, k = 1, l=0, flagIndex[arrMax],flagLenght;
- //匹配数据
- for (i = 0; data[i] != 0; ++i, j = 0){
- if (data[i] == _flag[j]){
- for (j = 1; _flag[j] != 0; ++j){
- if(_flag[j] != data[i+j]){//未能匹配到
- break;
- }
- }
- if (_flag[j] == 0){//匹配成功
- flagIndex[k-1]=i;
- i+=j;
- ++k;
- flagLenght=j;
- }
- }
- }
- flagIndex[k-1]=i;
- //分配空间
- paths = malloc(sizeof(char*)*k);
- paths[0] = malloc(sizeof(char) * flagIndex[0]);
- for(i=1; i<k; ++i){
- paths[i] = malloc(1+sizeof(char) * (flagIndex[i] - flagIndex[i-1]));
- }
- //抄入数据
- for(j = 0, l = 0; j < flagIndex[0]; ++j, ++l){
- paths[0][l]=data[j];
- }
- paths[0][l]=0;
- for(i = 0; i < k-1; ++i){
- for(j = flagIndex[i]+flagLenght, l = 0; j < flagIndex[i+1]; ++j, ++l){
- paths[i+1][l]=data[j];
- }
- paths[i+1][l]=0;
- }
- returnValue.paths=paths;
- returnValue.length=k;
- return returnValue;
- }
复制代码
我太菜了 |
|