#include"stdio.h"
char * str_str(char *des,char *str){
char *ptr1,*ptr2;
while(*des){
ptr1=des;
ptr2=str;
do{
if(*ptr2==0)
return des;
}while(*ptr1++==*ptr2++);
des++;
}
return NULL;
}
void parse(char* des,char item_cmd[10][10]){
int ide_i=0,ide_j=0;
char (*arr)[10];
char *ptr=str_str(des,"=");
ptr++;
printf("%s\n",ptr);
arr=item_cmd;
while(*ptr){
if(*ptr==','){
arr[ide_i][ide_j]=0;
printf("%s\n",arr[ide_i]);
ide_j=0;
ide_i++;
printf("\n");
}else{
arr[ide_i][ide_j]=*ptr;
ide_j++;
}
ptr++;
}
arr[ide_i][ide_j]=0;
printf("%s\n",arr[ide_i]);
}
void main(){
char item_cmd[10][10];
char *des="PWMDUTY=1000,0:100,1:200,2:300,3:400,4:500";
parse(des,item_cmd);
}
打印结果:
esp8266@esp8266-VirtualBox:~$ ./a.out
1000,0:100,1:200,2:300,3:400,4:500
1000
0:100
1:200
2:300
3:400
4:500
esp8266@esp8266-VirtualBox:~$