Copy a string

From Rosetta Code
Revision as of 14:16, 26 February 2007 by Ce (talk | contribs) (→‎[[C plus plus|C++]]: #include <string>)
Task
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.

Ada

Ada provides three diffferent 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;

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;

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

Compiler: DMD,GDC

Forth

Fortran

Java

 String src = "Hello";
 String dst = new String(s);

JavaScript

 var src = "Hello";
 var 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

Seed7

 var string: dest is "";
 dest := "Hello";

Tcl

set src "Rosetta Code"
set dst $src