Not logged in. Log in with GitHub. About Websheets.

List of all available Websheets


Viewing cpp/cstrings/strcpy by redekopp@usc.edu. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
PropertyValue
Description
html markup
shown to student
 
<p>
Define a function <tt>strcpy()</tt> that takes a 'src' character array and
'dest' character array as input.  <tt>src</tt> and <tt>dest</tt> are  null-terminated character arrays <strong>(aka C-strings).</strong></p> 
<strong>Note 1: the destination array should be the first argument, the source the second argument</strong>
<p>
   <strong>Note 2:</strong> The formal argument of a function taking an array usually requires <strong><tt>[]</tt> after the name (e.g. <tt>char src[]</tt>)</strong>
   but C/C++ supports a second way to accept an array in the formal argument using the <strong><tt>*</tt> (e.g. <tt>char *</tt>)</strong>
</p>
Engine
Template / Reference solution
 
#include <iostream>
using namespace std;
// Copy the characters from the src array to the destination array.
//  The arguments are passed as dest followed by src and both are
//  character arrays (C-Strings).  
void strcpy( \[ REDACTED ]\  dest, \[ REDACTED ]\ src  ) {
\[
REDACTED
]\
}
int main()
{
   char src1[12] = "hello world";
   char src2[10] = "bye";
   char src3[1]  = "";
   char dest[21] = "abcdefghijklmnopqrst";
   
   strcpy(dest, src1);
   cout << dest << endl;
   strcpy(dest, src2);
   cout << dest << endl;
   strcpy(dest, src3);
   cout << dest << endl;
   
   return 0;
}
C++ test suite
json list of stdin/args tests
e.g. [{"stdin":"hi", "args":["4", "5"]},
{"stdin":"noargs"}]

to just run once with no input use [{}]
 
[{}]


Optional properties:

Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.