Team Name:
Team Members:
Implement the C function countTokens according to the specification
given below. You must make use of the standard C functions strspn
and strcspn, which are also documented below. Your implementation
must be clearly written and as simple as possible - use only 1 loop and
no more than 15 lines of code.
size_t strspn(char* s, char* set)
returns the length of the initial portion of the string s that
consists of only characters contained in the string set.
size_t strcspn(char* s, char* set)
returns the length of the initial portion of the string s that
consists of only characters not contained in the string set.
/* Return the number of tokens in s. A token is a substring
s[i..j] where:
0 <= i <= j < strlen(s)-1 and
s[i..j] contains no character in delim and
either i == 0 or s[i-1] is in delim and
either j == strlen(2)-1 or s[j+1] is in delim. */
int countTokens(char* s, char* delim) {
put your answer here
char* s0;
int count = 0;
s0 = s;
s0 += strspn(s0,delim) /*skip over any leading delimiters*/
while (*s0 != '\0') {
s0 += strcspn(s0,delim); /*skip over token*/
s0 += strspn(s0,delim); /*skip over delimiters*/
count++;
}
return(count);
}