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

Discussion

What will be the output of the following C code?

const char str1[] = "abcdef";
const char str2[] = "fgha";
char *mat;
mat= strpbrk(str1, str2);
if(mat)
printf("First matching character: %c\n", *mat);
else
printf("Character not found");

 

  • A.g
  • B.a
  • C.h
  • D.f

Answer: D

Correct answer: a

strpbrk returns a pointer to the first character of str1 that occurs anywhere in str2. Scanning abcdef against the set {f, g, h, a}, the very first character, a, is already in the set - so the search stops there. The f would only be reached if a were not a member.

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

Post your comment

Please log in or register to post a comment.