Copy a string
You are encouraged to solve this task according to the task description, using any language you may know.
This task is about copying a string. Where it is relevant, distinguish between copying the contents of a string versus making an additional reference to an existing string.
Ada
Ada provides three different kinds of strings. The String type is a fixed length string. The Bounded_String type is a string with variable length up to a specified maximum size. The Unbounded_String type is a variable length string with no specified maximum size. The Bounded_String type behaves a lot like C strings, while the Unbounded_String type behaves a lot like the C++ String class.
Fixed Length String Copying.
Src : String := "Hello"; Dest : String := Src;
Ada provides the ability to manipulate slices of strings.
Src : String "Rosetta Stone"; Dest : String := Src(1..7); -- Assigns "Rosetta" to Dest Dest2 : String := Src(9..13); -- Assigns "Stone" to Dest2
Bounded Length String Copying
-- Instantiate the generic package Ada.Strings.Bounded.Generic_Bounded_Length with a maximum length of 80 characters package Flexible_String is new Ada.Strings.Bounded.Generic_Bounded_Length(80); use Flexible_String; Src : Bounded_String := To_Bounded_String("Hello"); Dest : Bounded_String := Src;
Ada Bounded_String type provides a number of functions for dealing with slices.
Unbounded Length String Copying
-- The package Ada.Strings.Unbounded contains the definition of the Unbounded_String type and all its methods Src : Unbounded_String := To_Unbounded_String("Hello"); Dest : Unbounded_String := Src;
ALGOL 68
In ALGOL 68 strings are simply flexible length arrays of CHAR;
( STRING src:="Hello", dest; dest:=src )
BASIC
Interpeter: QuickBasic 4.5, PB 7.1
src$ = "Hello" 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;
Objective-C
Immutable strings - since they are immutable, you may get the same instance with its references count increased.
NSString *original = @"Literal String" NSString *new = [original copy];
Mutable strings - you can get either new mutable or immutable string:
NSMutableString *original = @"Literal String" NSString *immutable = [original copy]; NSMutableString *mutable = [original mutableCopy];
Copying a CString:
char *cstring = "I'm a plain C string"; NSString *string = [NSString stringWithUTF8String:cstring];
Copying from data, possibly not null terminated:
char bytes[] = "some data"; NSString *string = [[NSString alloc] initWithBytes:bytes length:9 encoding:NSASCIIStringEncoding];
And of course, if a C string is needed, you can use standard functions like strcpy.
C++
Libraries: C++ Standard Library
#include <string> 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
char[] src = "string"; char[] dest = src.dup;
Erlang
Src = "Hello". Dst = Src.
Forth
Forth strings are generally stored in memory as prefix counted string, where the first byte contains the string length. However, on the stack they are most often represented as <addr cnt> pairs. Thus the way you copy a string depends on where the source string comes from:
\ Allocate two string buffers create stringa 256 allot create stringb 256 allot \ Copy a constant string into a string buffer s" Hello" stringa place \ Copy the contents of one string buffer into another stringa count stringb place
Java
String src = "Hello"; String newAlias = src; String strCopy = new String(src); //"newAlias == src" is true //"strCopy == src" is false //"strCopy.equals(src)" is true
JavaScript
var src = "Hello"; var dst = src;
Lisp
(setf orig "Hello") (setf copied (copy-seq orig))
test the copy: (equalp orig copied) t (eq orig copied) nil
Perl
Interpeter: Perl
my $src = "Hello"; my $dst = $src;
PHP
$src = "Hello"; $dst = $src;
Pop11
In Pop11 normal data are represented by references, so plain assignment will copy references. To copy data one has to use copy procedure:
vars src, dst; 'Hello' -> src; copy(src) -> dst;
One can also combine assignment (initialization) with variable declarations:
vars src='Hello'; vars dst=copy(src);
Python
Interpeter: Python 2.3, 2.4, 2.5
Since strings are immutable, all copy operations return the same string. Probably the reference is increased.
>>> src = "hello" >>> a = src >>> b = src[:] >>> import copy >>> c = copy.copy(src) >>> d = copy.deepcopy(src) >>> src is a is b is c is d True
To actually copy a string:
>>> a = 'hello' >>> b = ''.join(a) >>> a == b True >>> b is a False
As a result of object "interning" some strings such as the empty string and single character strings like 'a' may be references to the same object regardless of copying. This can potentially happen with any Python immutable object and should be of no consequence to any proper code.
Be careful with 'is' - use it only when you want to compare the identity of the object. To compare string values, use '=='.
Raven
Copy a string by reference:
'abc' as a a as b
Copy a string by value:
'abc' as a a copy as b
Ruby
src="Hello" dst=src.dup
Seed7
var string: dest is "";
dest := "Hello";
Tcl
set src "Rosetta Code" set dst $src
Toka
" hello" is-data a a string.clone is-data b
Visual Basic .NET
Platform: .NET
Language Version: 9.0+
'Immutable Strings Dim a = "Test string" Dim b = a 'reference to same string Dim c = New String(a.ToCharArray) 'new string, normally not used 'Mutable Strings Dim x As New Text.StringBuilder("Test string") Dim y = x 'reference Dim z = New Text.StringBuilder(x.ToString) 'new string