split char string with multi-character delimiter in C

前端 未结 3 703
轮回少年
轮回少年 2020-12-07 04:14

I want to split a char *string based on multiple-character delimiter. I know that strtok() is used to split a string but it works with single chara

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-07 04:28

    EDIT : Considered suggestions from Alan and Sourav and written a basic code for the same .

    #include 
    
    #include 
    
    int main (void)
    {
      char str[] = "This is abc test abc string";
    
      char* in = str;
      char *delim = "abc";
      char *token;
    
      do {
    
        token = strstr(in,delim);
    
        if (token) 
          *token = '\0';
    
        printf("%s\n",in);
    
        in = token+strlen(delim);
    
      }while(token!=NULL);
    
    
      return 0;
    }
    

提交回复
热议问题