+ 4

C challenge question, true or false?

One of the questions goes like this: The strcat() function copies the string to the another character array. available answers: True, False What's your opinion about it? if you pick an answer please mention why? clearly it can't be both true and false.

12th Sep 2022, 9:55 AM
Tina
Tina - avatar
24 Réponses
+ 5
The problem with many of these challenge questions is that they have been written by kidz hungry for badges. Kidz who don't know anything about C, or perhaps just not enough. The first set of languages had their challenge questions a long time already. It was nigh impossible to get new ones approved. When C was integrated into the challenge system, people went nuts! It was the one chance to get the challenge creator badges for approved challenges. The result of which you have now seen. That said: does strcat copy the source to the destination? Yes. Question has wrong answer. Case closed. Can strcat be use to simulate strcpy? Yes. Can strcpy be used to simulate strcat? Yes. Essentially, they are equivalent. They only exist for convenience and easier source code reading.
13th Sep 2022, 3:46 PM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 2
Quoted from http://www.cplusplus.com/reference/cstring/strcat/ "Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination." So yes, it appends one string to another. And yes, it is done by copying the characters.
12th Sep 2022, 10:39 AM
Ipang
+ 2
I think it's False. "*Appends* string to the another character array" is the correct statement. Copying will override destination. And there is special function for that strcpy(). So question is to meant deferenciate these two, I guess.
12th Sep 2022, 11:22 AM
Jayakrishna 🇮🇳
+ 2
Tina 🕊🇺🇦 I'm just quoting someone here in my earlier days, he said that challenges are merely about memorizing the answers. So I guess that's that ... 😁
12th Sep 2022, 4:37 PM
Ipang
+ 2
Tina 🕊🇺🇦 See if I say, copy array => it mean do copy. Next time I say : extend array => does it mean dont copy? No. "The strcat() function copies the string to the another character array." -Main verb is "copies". and taking about strcat().. So it is about purpose. If I say, true then technically wrong. strcpy() is there for that purpose. Ok. In your logical sence, false qualifies, in the benefit of doubt. . False does not mean it don't use copy. It means only Statement is not completely true .. see a = b also copying but we say assigning.. 'Extends' includes copy, we don't say copy at end. 'append', cancat,. . involves copy, actually may not copy always.. It adds at end. If we use a array as sourse then we say copying from array. If we have a value, then we say adding at end. No copying.. a = b copy but a = 2 is assignment. Same with strcat() . strcat( array1, array2); strcat(array1, "adding"); False doesn't mean it never use copy. It may but statement is not true.
12th Sep 2022, 7:06 PM
Jayakrishna 🇮🇳
+ 2
Tina 🕊🇺🇦 This is an example may help you Function header: char *strcat(char *destination, const char *source) example: char str1[100] = "This is ",str2[] = "sololearn.com"; strcat(str1, str2); // output "This is sololearn.com" puts(str1);//This is sololearn.com puts(str2);//sololearn.com so the function concatenate the two strings and store them in str1 But strcpy(str1,str2); copy the content of str2 into str1 puts(str1);//sololearn.com puts(str2);//sololearn.com that is the difference Concatenate and copying The answer is : False
13th Sep 2022, 12:35 AM
Aly Alsayed
Aly Alsayed - avatar
+ 2
Aly Alsayed and how this concatenation is possible without copying? "The strcat() function copies the string..." so the answer is: True Or is there any other way to concatenate that I'm not aware of?
13th Sep 2022, 1:09 AM
Tina
Tina - avatar
+ 2
Tina 🕊🇺🇦 strcat(str1,str2) // copy str2 and add or(appends) it to the end of str1 that is called concatenation strcpy(str1,str2) // copy str2 into str1 but now the old string had been deleted and new string had been added (no appending or concat) that called copy think of it like you copy and paste in word file If u select all content in word file and delete all then paste words from source that called "copying" but if you copy and paste ur new words to the end of the content that called "concatenation"
13th Sep 2022, 11:00 AM
Aly Alsayed
Aly Alsayed - avatar
+ 2
Aly Alsayed Jayakrishna🇮🇳 let's first agree on some fundamental concepts and stop playing with words to have a conclusion. I assume that we all agree that: copy means making pair of identical values in memory at different addresses. it could be 0 to N pairs. it's simple and straight. back to question: a strcpy(s1,s2) copies entire contents of s2 to s1, starting at the first index of s1. a strcat(s1,s2) copies entire contents of s2 to s1 starting at the last index of s1 Q: strcat copies s2 to s1. A: False??? Jayakarishna, what is wrong with this Q is that infact it's not a question but an obvious fact which has ignored by whoever designed it. some sources: char *strcat(char *dest, const char *src) { char *ret = dest; while (*dest) dest++; while ((*dest++ = *src++))    ; return ret; } ____ #include <string.h> #undef strcat #ifndef STRCAT #define STRCAT strcat #endif /* Append SRC on the end of DEST.  */ char * STRCAT (char *dest, const char *src) {   strcpy (dest + strlen (dest), src);   return dest; }
13th Sep 2022, 3:41 PM
Tina
Tina - avatar
+ 2
Ani Jona 🕊 nailed it!
13th Sep 2022, 3:50 PM
Tina
Tina - avatar
+ 2
I believe that without the copy it would not be possible to concatenate by the function
13th Sep 2022, 4:00 PM
Francisco Diêgo Pontes de Sousa
Francisco Diêgo Pontes de Sousa - avatar
+ 1
Ipang "yes, it is done by copying the characters" I think so, therefore the correct answer must be "true" but accepted answer is "false".
12th Sep 2022, 11:26 AM
Tina
Tina - avatar
+ 1
Tina 🕊🇺🇦 Question says "copies" .. That's why it is wrong. So i answer "false". There is a special function for copying strcpy(). and this is difference between these two functions "Append vs copy". In the case destination is empty, it is a special case, not a general case. Still it is overwrites empty string.. So it is an approach to using strcat() in place of strcpy() by the users. strcat() *uses* copying from end of destination, and not overrites. but strcpy() just copies from start, in this case destination will get replaced if have any value. So strcat() purpose is appending while strcpy purpose is copying.. So question is about the function purpose, but not it's internal logic. No question is made in indirect logic base I think. So i say, not copies but appends. (You may be meaning like thinking, copies is also right then it is only half answer. *Copies at end* is full ( means appends) ) is right answer... Is not it..!?
12th Sep 2022, 12:14 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 Yes I'm certainly aware of differences between strcpy strcat strncat strncpy... and what was the purpose of question. let's put it this way: if answer to the question is "false" strcat function doesn't copy source to destination, neither to the end of it nor wherever else. no memory read/write has been done. if strcat actually copies from source to destination (which is the reality of what it does) then the answer must be true. the whole point for me is thinking logically. Question just mentions "copies" (not copies at the end, nor appending, nor concatenation) and expects a "no, it doesn't copy" answer, while strcat actually does copy.
12th Sep 2022, 1:13 PM
Tina
Tina - avatar
+ 1
Yes, Quantum got a good point, some questions are sort of "insane" 😁 And I guess it's just the same for quizzes in other languages also. I can confirm this because I've seen people questioning whether some quizzes were correct. And like you, they reported the faulty ones though Idk what happens afterwards ...
12th Sep 2022, 2:20 PM
Ipang
+ 1
Tina 🕊🇺🇦 I only can say that "I would think the same as you if I don't know about strcpy() and difference between strcpy() and strcat()." And i can see question is about purpose, unless it includes "...internal logically" as well at end. What's your answer to these: The strcat() function ________ the string to the another character array. The strcpy() function ________ the string to the another character array. and form true/false types question yourself. You know, same answer to both will not qualify. I can't find a better example now.. 😌
12th Sep 2022, 4:03 PM
Jayakrishna 🇮🇳
+ 1
Ipang yes, however this is just an introductory question in C, nothing fancy or complicated. if instead of: "The strcat() function copies the string to the another character array." it was: "The strcat() function is used to copy a string" it was way more clear that it is designed to differentiate between strcat and strcpy. AND there wasn't this much headache! https://man7.org/linux/man-pages/man3/strcpy.3.html https://man7.org/linux/man-pages/man3/strcat.3.html
12th Sep 2022, 4:21 PM
Tina
Tina - avatar
+ 1
Jayakrishna🇮🇳 thank you for your contribution, about the question purpose see my last reply to Ipang and I'm totally agree with you. if the answer is true (strcat copies) the difference between the two is strcat first searches for end of the string and then copies while strcpy won't do this step hence, a != b BUT if you say false, then strcat "doesn't copy" at all according to the question statement. about your questions, strcat: appends, copy starts from null terminator of the destination. strcpy: copies, copy starts from the beginning. as you see my answers are not the same, no one doubts about these but does "strcat() function copies the string..." ? of course yes, it copies -> true. otherwise: strcat() doesn't copy -> false. then it must be a dummy function. I highly doubt that a third option exists here.
12th Sep 2022, 5:30 PM
Tina
Tina - avatar
+ 1
strcpy : copy sourse to destination strcat : appends source to destination. Yes. Stop playing with words "copies", think of it's purpose only. my previously post fill in the blanks and form true/false types question yourself. I think, these are correct statements without any flaws. So you can fill with one word answer. And make a true/false type question. So that may answer itself, the question designer view. That's it nothing else.
13th Sep 2022, 4:42 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 The question says "copies" if it was "appends" as you mentioned, there wouldn't be any problem. Now either the question is wrong or the answer. ps. Copy won't necessarily overwrite, in the case that destination is empty so one may use strcat in place of strcpy which again is copying.
12th Sep 2022, 11:48 AM
Tina
Tina - avatar