Machine-translated by Google. Questions are authored in English.
Top

Discussion

What will be the output of the following C code?

const char str1[]="ABCDEF1234567";
const char str2[] = "269";
len = strcspn(str1, str2);
printf("First matching character is at %d\n", len + 1);

 

  • A.First matching character is at 8
  • B.First matching character is at 7
  • C.First matching character is at 9
  • D.First matching character is at 12

Answer: A

Correct answer: First matching character is at 8

strcspn returns the length of the initial segment of str1 containing no character from str2. Scanning ABCDEF1234567 against 269, the first seven characters ABCDEF1 are all absent from the set, and the eighth, 2, is present. So the function returns 7, and the program prints 7 + 1 = 8 - the 1-based position of the first match.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.