Is there a way to count tokens in C?

谁说我不能喝 提交于 2020-01-01 02:35:09

问题


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 need to split it and pass the arguments to execve() .

Thanks!

Edit

execve takes arguments as char**, so I need to allocate an array of pointers. I don't know how many to allocate without knowing how many tokens are there.


回答1:


One approach would be to simply use strtok with a counter. However, that will modify the original string.

Another approach is to use strchr in a loop, like so:

int count = 0;
char *ptr = s;
while((ptr = strchr(ptr, ' ')) != NULL) {
    count++;
    ptr++;
}

If you have multiple delimiters, use strpbrk:

while((ptr = strpbrk(ptr, " \t")) != NULL) ...



回答2:


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<strlen(str);i++){
    if(str[i] == ' ')
        count++;
}

No. of tokens would be same as count+1




回答3:


Here is a version based on strtok which does not modify the original string, but a temporal copy of it. This version works for any combination of tabs and space characters used as tokens separators. The function is

unsigned long int getNofTokens(const char* string){
  char* stringCopy;
  unsigned long int stringLength;
  unsigned long int count = 0;

  stringLength = (unsigned)strlen(string);
  stringCopy = malloc((stringLength+1)*sizeof(char));
  strcpy(stringCopy,string);

  if( strtok(stringCopy, " \t") != NULL){
    count++;
    while( strtok(NULL," \t") != NULL )
        count++;
  }

  free(stringCopy);
  return count;
}

A function call could be

char stringExample[]=" wordA 25.4 \t 5.6e-3\t\twordB 4.5e005\t ";
printf("number of elements in stringExample is %lu",getNofTokens(stringExample));

Output is

number of elements in stringExample is 5


来源:https://stackoverflow.com/questions/13078926/is-there-a-way-to-count-tokens-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!