Using Pointers and strtok()

前端 未结 2 1321
借酒劲吻你
借酒劲吻你 2021-01-25 15:26

I\'m building a linked list and need your assistance please as I\'m new to C. I need to input a string that looks like this: (word)_#_(year)_#_(DEFINITION(UPPER CASE))

2条回答
  •  花落未央
    2021-01-25 15:46

    #include 
    #include 
    
    char *strtokByWord_r(char *str, const char *word, char **store){
        char *p, *ret;
        if(str != NULL){
            *store = str;
        }
        if(*store == NULL) return NULL;
        p = strstr(ret=*store, word);
        if(p){
            *p='\0';
            *store = p + strlen(word);
        } else {
            *store = NULL;
        }
        return ret;
    }
    char *strtokByWord(char *str, const char *word){
        static char *store = NULL;
        return strtokByWord_r(str, word, &store);
    }
    
    int main(){
        char input[]="invest_#_1945_#_TRADE";
        char *array[3];
        char *p;
        int i, size = sizeof(array)/sizeof(char*);
        for(i=0, p=input;i

提交回复
热议问题