To copy a string into another, given:
char s1[30] = "xyz", s2[20];
-
s2 = s1;
-
strcpy(s2,s1);
-
s2 == s1;
-
all three are wrong
In C, strings are arrays and cannot be assigned using the '=' operator. The 'strcpy' function from the string.h library is the standard way to copy the contents of one character array (s1) into another (s2), including the null terminator.
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.