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

Discussion

What will be the output of the following C code?

const char str1[10]="Helloworld";
const char str2[10] = "world";
char *mat;
mat = strstr(str1, str2);
printf("The substring is:%s\n", mat);

 

  • A.The substring is:world
  • B.The substring is:Hello
  • C.The substring is:Helloworld
  • D.error in the code

Answer: A

Correct answer: The substring is:world

strstr(str1, str2) finds the first occurrence of "world" inside "Helloworld" and returns a pointer to where it begins - not a copy. Printing from that pointer therefore gives world, the rest of the string from the match onwards.

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

Post your comment

Please log in or register to post a comment.