Is there a way to count tokens in C?

前端 未结 3 1630
遥遥无期
遥遥无期 2021-02-08 08:00

I\'m using strtok to split a string into tokens. Does anyone know any function which actually counts the number of tokens?

I have a command string and I nee

3条回答
  •  花落未央
    2021-02-08 08:25

    As number of tokens is nothing but one more than the frequency of occurrence of the delimiter used. So your question boils down to find no. of times of occurrence of a character in a string

    say the delimiter used in strtok function in c is ' '

    int count =0,i;
    char str[20] = "some string here";
    
    for(i=0;i

    No. of tokens would be same as count+1

提交回复
热议问题