A bit more elegant:
Code:
#include <stdio.h>
#include <string.h>
int main(void) {
int tokenNum=0, len;
char line[]={"a b c d e"};
len = strlen(line);
tokenNum = (len+1)/2;
printf("Line: %s, length: %d, tokenNum: %d\n",line, len, tokenNum);
return 0;
}
Note that the above requires a string of char that is strictly formatted as you've described - alternating one char and one space, and no newlines or other punctuation present.
If you are using fgets, you are adding a newline to the string. Remove it by adding this line of code:
Code:
if(line[len]-1=='\n)
line[len]-1= '\0';
Which will overwrite the newline, and make things work correctly again. Put this in, right after the "len = strlen(line);" line of code.