To copy a string into another, you can use the strcpy()
function from the string.h library in C.
Let's go through each option to understand why it is correct or incorrect:
Option A) s2 = s1; - This option is incorrect because it assigns the memory address of s1
to s2
, rather than copying the contents of s1
into s2
.
Option B) strcpy(s2, s1); - This option is correct. The strcpy()
function is used to copy the contents of one string (s1
) into another string (s2
). It takes two arguments: the destination string (s2
) and the source string (s1
).
Option C) s2 == s1; - This option is incorrect. The ==
operator is used to compare the memory addresses of two variables, not their contents.
Option D) all three are wrong - This option is incorrect because option B is correct.
Therefore, the correct answer is B) strcpy(s2, s1);. This option correctly copies the contents of s1
into s2
using the strcpy()
function.