Copy a string: Difference between revisions
Content deleted Content added
-> Tcl |
|||
Line 121: | Line 121: | ||
dest := "Hello"; |
dest := "Hello"; |
||
==[[Tcl]]== |
|||
[[Category:Tcl]] |
|||
set src "Rosetta Code" |
|||
set dst $src |
Revision as of 05:58, 24 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.
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: 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
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