ProgrammingVery hardProduct company OA

What is printed?

What is printed?
char s[] = "a,b,,c";
int n = 0;
for (char *t = strtok(s, ","); t; t = strtok(NULL, ","))
    n++;
printf("%d %zu %c", n, strlen(s), s[2]);
  1. A. 4 1 b
  2. B. 3 6 ,
  3. C. 3 1 b
  4. D. 4 6 b

Correct answer: C. 3 1 b

strtok treats runs of delimiters as one separator and never returns empty tokens, so the tokens are "a", "b", "c": n = 3. It also writes '\0' over the delimiter that ends each token, so s[1] becomes '\0' and strlen(s) is now 1. s[2] is still 'b'. This is why strtok must never be given a string literal.

More C Programming questions

Exams that ask this topic