Copy a string: Difference between revisions
Content deleted Content added
Line 36: | Line 36: | ||
dst[79] = 0; |
dst[79] = 0; |
||
==[[C plus plus]]== |
==[[C plus plus|C++]]== |
||
[[Category:C plus plus]] |
[[Category:C plus plus]] |
||
'''Libraries:''' [[STL]] |
'''Libraries:''' [[STL]] |
Revision as of 23:59, 22 February 2007
Copy a string
You are encouraged to solve this task according to the task description, using any language you may know.
You are encouraged to solve this task according to the task description, using any language you may know.
Java
String src = "Hello"; String dst = new String(s);
JavaScript
var src = "Hello"; var dst = src;
C
// Using strdup const char* src = "Hello"; char* dst = strdup(src);
// Using malloc/strcpy const char* src = "Hello"; int len = strlen(src); char* dst = (char*)malloc(len+1); strcpy(dst, src);
// Using malloc/strncpy const char* src = "Hello"; int len = strlen(src); char* dst = (char*)malloc(len+1); strncpy(dst, src, len+1); dst[len] = 0;
// Using static buffer const char* src= "Hello"; static char dst[80]; strncpy(dst, src, 80); dst[79] = 0;
C++
Libraries: STL
// STL std::string src = "Hello"; std::string dst = src;
Libraries: Qt
// Qt QString src = "Hello"; QString dst = src;
Libraries: Microsoft Foundation Classes
// MFC CString src = "Hello"; CString dst = src;
C#
string src = "Hello"; string dst = src;
D
Forth
Fortran
BASIC
Interpeter: QuickBasic 4.5, PB 7.1
src$ = "Hello" dst$ = src$
Perl
Interpeter: Perl
my $src = "Hello"; my $dst = $src;
PHP
$src = "Hello; $dst = $src;
Python
Interpeter: Python 2.3, 2.4, 2.5