tasks a-s

This commit is contained in:
Ingy döt Net 2013-04-10 23:57:08 -07:00
parent 47bf37c096
commit b83f433714
12433 changed files with 156208 additions and 123 deletions

View file

@ -0,0 +1 @@
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.

View file

@ -0,0 +1,4 @@
---
category:
- String manipulation
note: Basic language learning

View file

@ -0,0 +1,3 @@
data: lv_string1 type string value 'Test',
lv_string2 type string.
lv_string2 = lv_string1.

View file

@ -0,0 +1,4 @@
(
STRING src:="Hello", dest;
dest:=src
)

View file

@ -0,0 +1,6 @@
BEGIN {
a = "a string"
b = a
sub(/a/, "X", a) # modify a
print b # b is a copy, not a reference to...
}

View file

@ -0,0 +1,2 @@
var str1:String = "Hello";
var str2:String = str1;

View file

@ -0,0 +1,2 @@
Src : String := "Hello";
Dest : String := Src;

View file

@ -0,0 +1,3 @@
Src : String := "Rosetta Stone";
Dest : String := Src(1..7); -- Assigns "Rosetta" to Dest
Dest2 : String := Src(9..13); -- Assigns "Stone" to Dest2

View file

@ -0,0 +1,6 @@
-- 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;

View file

@ -0,0 +1,3 @@
-- 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;

View file

@ -0,0 +1,2 @@
src := "Hello"
dst := src

View file

@ -0,0 +1,2 @@
$Src= "Hello"
$dest = $Src

View file

@ -0,0 +1,11 @@
source$ = "Hello, world!"
REM Copy the contents of a string:
copy$ = source$
PRINT copy$
REM Make an additional reference to a string:
!^same$ = !^source$
?(^same$+4) = ?(^source$+4)
?(^same$+5) = ?(^source$+5)
PRINT same$

View file

@ -0,0 +1,2 @@
set src=Hello
set dst=%src%

View file

@ -0,0 +1,8 @@
abcdef:?a;
!a:?b;
c=abcdef;
!c:?d;
!a:!b { variables a and b are the same and probably referencing the same string }
!a:!d { variables a and d are also the same but not referencing the same string }

View file

@ -0,0 +1,13 @@
#include <iostream>
#include <string>
#include <algorithm>
int main( ) {
std::string original ( "This is the original" ) ;
std::string mycopy( original.length( ) , ' ' ) ;
std::copy ( original.begin( ) , original.end( ) , mycopy.begin( ) ) ;
std::cout << "This is the copy: " << mycopy << std::endl ;
original.assign( "Now we change the original! " ) ;
std::cout << "mycopy still is " << mycopy << std::endl ;
return 0 ;
}

View file

@ -0,0 +1,60 @@
#include <stdlib.h> /* exit(), free() */
#include <stdio.h> /* fputs(), perror(), printf() */
#include <string.h>
int
main()
{
size_t len;
char src[] = "Hello";
char dst1[80], dst2[80];
char *dst3, *ref;
/*
* Option 1. Use strcpy() from <string.h>.
*
* DANGER! strcpy() can overflow the destination buffer.
* strcpy() is only safe if the source string is shorter than
* the destination buffer. We know that "Hello" (6 characters
* with the final '\0') easily fits in dst1 (80 characters).
*/
strcpy(dst1, src);
/*
* Option 2. Use strlen() and memcpy() from <string.h>, to copy
* strlen(src) + 1 bytes including the final '\0'.
*/
len = strlen(src);
if (len >= sizeof dst2) {
fputs("The buffer is too small!\n", stderr);
exit(1);
}
memcpy(dst2, src, len + 1);
/*
* Option 3. Use strdup() from <string.h>, to allocate a copy.
*/
dst3 = strdup(src);
if (dst3 == NULL) {
/* Failed to allocate memory! */
perror("strdup");
exit(1);
}
/* Create another reference to the source string. */
ref = src;
/* Modify the source string, not its copies. */
memset(src, '-', 5);
printf(" src: %s\n", src); /* src: ----- */
printf("dst1: %s\n", dst1); /* dst1: Hello */
printf("dst2: %s\n", dst2); /* dst2: Hello */
printf("dst3: %s\n", dst3); /* dst3: Hello */
printf(" ref: %s\n", ref); /* ref: ----- */
/* Free memory from strdup(). */
free(dst3);
return 0;
}

View file

@ -0,0 +1,22 @@
#include <stdlib.h> /* exit() */
#include <stdio.h> /* fputs(), printf() */
#include <string.h>
int
main()
{
char src[] = "Hello";
char dst[80];
/* Use strlcpy() from <string.h>. */
if (strlcpy(dst, src, sizeof dst) >= sizeof dst) {
fputs("The buffer is too small!\n", stderr);
exit(1);
}
memset(src, '-', 5);
printf("src: %s\n", src); /* src: ----- */
printf("dst: %s\n", dst); /* dst: Hello */
return 0;
}

View file

@ -0,0 +1,3 @@
(let [s "hello"
s1 s]
(println s s1))

View file

@ -0,0 +1,2 @@
<cfset stringOrig = "I am a string." />
<cfset stringCopy = stringOrig />

View file

@ -0,0 +1,10 @@
(let* ((s1 "Hello") ; s1 is a variable containing a string
(s1-ref s1) ; another variable with the same value
(s2 (copy-seq s1))) ; s2 has a distinct string object with the same contents
(assert (eq s1 s1-ref)) ; same object
(assert (not (eq s1 s2))) ; different object
(assert (equal s1 s2)) ; same contents
(fill s2 #\!) ; overwrite s2
(princ s1)
(princ s2)) ; will print "Hello!!!!!"

View file

@ -0,0 +1,12 @@
void main() {
string src = "This is a string";
// copy contents:
auto dest1 = src.idup;
// copy contents to mutable char array
auto dest2 = src.dup;
// copy just the fat reference of the string
auto dest3 = src;
}

View file

@ -0,0 +1,15 @@
program CopyString;
{$APPTYPE CONSOLE}
var
s1: string;
s2: string;
begin
s1 := 'Goodbye';
s2 := s1; // S2 points at the same string as S1
s2 := s2 + ', World!'; // A new string is created for S2
Writeln(s1);
Writeln(s2);
end.

View file

@ -0,0 +1,2 @@
Src = "Hello".
Dst = Src.

View file

@ -0,0 +1,2 @@
sequence first = "ABC"
sequence newOne = first

View file

@ -0,0 +1,4 @@
"This is a mutable string." dup ! reference
"Let's make a deal!" dup clone ! copy
"New" " string" append . ! new string
"New string"

View file

@ -0,0 +1,2 @@
SBUF" Grow me!" dup " OK." append
SBUF" Grow me! OK."

View file

@ -0,0 +1,2 @@
SBUF" I'll be a string someday." >string .
"I'll be a string someday."

View file

@ -0,0 +1,9 @@
\ 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

View file

@ -0,0 +1 @@
str2 = str1

View file

@ -0,0 +1,13 @@
#In GAP strings are lists of characters. An affectation simply copy references
a := "more";
b := a;
b{[1..4]} := "less";
a;
# "less"
# Here is a true copy
a := "more";
b := ShallowCopy(a);
b{[1..4]} := "less";
a;
# "more"

View file

@ -0,0 +1,2 @@
src = "string"
dest = src

View file

@ -0,0 +1,3 @@
Start.Programs,Accessories,Notepad,
Type:Hello world[pling],Highlight:Hello world[pling],
Menu,Edit,Copy,Menu,Edit,Paste

View file

@ -0,0 +1,4 @@
DIM src AS String
DIM dst AS String
src = "Hello"
dst = src

View file

@ -0,0 +1,2 @@
src := "Hello"
dst := src

View file

@ -0,0 +1,3 @@
def string = 'Scooby-doo-bee-doo' // assigns string object to a variable reference
def stringRef = string // assigns another variable reference to the same object
def stringCopy = new String(string) // copies string value into a new object, and assigns to a third variable reference

View file

@ -0,0 +1,4 @@
assert string == stringRef // they have equal values (like Java equals(), not like Java ==)
assert string.is(stringRef) // they are references to the same objext (like Java ==)
assert string == stringCopy // they have equal values
assert ! string.is(stringCopy) // they are references to different objects (like Java !=)

View file

@ -0,0 +1,2 @@
src = "Hello World"
dst = src

View file

@ -0,0 +1,6 @@
procedure main()
a := "qwerty"
b := a
b[2+:4] := "uarterl"
write(a," -> ",b)
end

View file

@ -0,0 +1,2 @@
src =: 'hello'
dest =: src

View file

@ -0,0 +1,7 @@
String src = "Hello";
String newAlias = src;
String strCopy = new String(src);
//"newAlias == src" is true
//"strCopy == src" is false
//"strCopy.equals(src)" is true

View file

@ -0,0 +1 @@
StringBuffer srcCopy = new StringBuffer("Hello");

View file

@ -0,0 +1,4 @@
var container = {myString: "Hello"};
var containerCopy = container; // Now both identifiers refer to the same object
containerCopy.myString = "Goodbye"; // container.myString will also return "Goodbye"

View file

@ -0,0 +1,4 @@
var a = "Hello";
var b = a; // Same as saying window.b = window.a
b = "Goodbye" // b contains a copy of a's value and a will still return "Hello"

View file

@ -0,0 +1 @@
"hello" dup

View file

@ -0,0 +1,2 @@
Var:String str1 = "Hello";
Var:String str2 = str1;

View file

@ -0,0 +1 @@
'hello dup

View file

@ -0,0 +1,4 @@
src$ = "Hello"
dest$ = src$
print src$
print dest$

View file

@ -0,0 +1,9 @@
+ scon : STRING_CONSTANT;
+ svar : STRING;
scon := "sample";
svar := STRING.create 20;
svar.copy scon;
svar.append "!\n";
svar.print;

View file

@ -0,0 +1,10 @@
make "a "foo
make "b "foo
print .eq :a :b ; true, identical symbols are reused
make "c :a
print .eq :a :c ; true, copy a reference
make "c word :b "|| ; force a copy of the contents of a word by appending the empty word
print equal? :b :c ; true
print .eq :b :c ; false

View file

@ -0,0 +1,4 @@
a = "string"
b = a
print(a == b) -->true
print(b) -->string

View file

@ -0,0 +1,2 @@
string1 = 'Hello';
string2 = string1;

View file

@ -0,0 +1,2 @@
str1 = "Hello"
str2 = copy str1

View file

@ -0,0 +1,2 @@
SET S1="Greetings, Planet"
SET S2=S1

View file

@ -0,0 +1,13 @@
> s := "some string";
s := "some string"
> t := "some string";
t := "some string"
> evalb( s = t ); # they are equal
true
> addressof( s ) = addressof( t ); # not just equal data, but the same address in memory
3078334210 = 3078334210
> u := t: # copy reference

View file

@ -0,0 +1,2 @@
a="Hello World"
b=a

View file

@ -0,0 +1,19 @@
/* It's possible in Maxima to access individual characters by subscripts, but it's not the usual way.
Also, the result is "Lisp character", which cannot be used by other Maxima functions except cunlisp. The usual
way to access characters is charat, returning a "Maxima character" (actually a one characte string). With the latter,
it's impossible to modify a string in place, thus scopy is of little use. */
a: "loners"$
b: scopy(a)$
c: a$
c[2]: c[5]$
a;
"losers"
b;
"loners"
c;
"losers"

View file

@ -0,0 +1,7 @@
string s, a;
s := "hello";
a := s;
s := s & " world";
message s; % writes "hello world"
message a; % writes "hello"
end

View file

@ -0,0 +1,8 @@
src = "Hello"
new_alias = src
puts 'interned strings are equal' if src == new_alias
str_copy = String.new(src)
puts 'non-interned strings are not equal' if str_copy != src
puts 'compare strings with equals()' if str_copy.equals(src)

View file

@ -0,0 +1,2 @@
VAR src: TEXT := "Foo";
VAR dst: TEXT := src;

View file

@ -0,0 +1,18 @@
using System;
using System.Console;
using Nemerle;
module StrCopy
{
Main() : void
{
mutable str1 = "I am not changed"; // str1 is bound to literal
def str2 = lazy(str1); // str2 will be bound when evaluated
def str3 = str1; // str3 is bound to value of str1
str1 = "I am changed"; // str1 is bound to new literal
Write($"$(str1)\n$(str2)\n$(str3)\n"); // str2 is bound to value of str1
// Output: I am changed
// I am changed
// I am not changed
}
}

View file

@ -0,0 +1,10 @@
/* NetRexx */
options replace format comments java crossref symbols nobinary
s1 = 'This is a Rexx string'
s2 = s1
s2 = s2.changestr(' ', '_')
say s1
say s2

View file

@ -0,0 +1,6 @@
(define (assert f msg) (if (not f) (println msg)))
(setq s "Greetings!" c (copy s))
(reverse c) ; Modifies c in place.
(assert (= s c) "Strings not equal.")

View file

@ -0,0 +1 @@
let dst = String.copy src

View file

@ -0,0 +1,10 @@
MODULE CopyString;
TYPE
String = ARRAY 128 OF CHAR;
VAR
a,b: String;
BEGIN
a := "plain string";
COPY(a,b);
END CopyString.

View file

@ -0,0 +1,2 @@
a := "GoodBye!";
b := a;

View file

@ -0,0 +1,4 @@
NSString *original = @"Literal String";
NSString *new = [original copy];
NSString *anotherNew = [NSString stringWithString:original];
NSString *newMutable = [original mutableCopy];

View file

@ -0,0 +1,4 @@
NSMutableString *original = [NSMutableString stringWithString:@"Literal String"];
NSString *immutable = [original copy];
NSString *anotherImmutable = [NSString stringWithString:original];
NSMutableString *mutable = [original mutableCopy];

View file

@ -0,0 +1,2 @@
const char *cstring = "I'm a plain C string";
NSString *string = [NSString stringWithUTF8String:cstring];

View file

@ -0,0 +1,2 @@
char bytes[] = "some data";
NSString *string = [[NSString alloc] initWithBytes:bytes length:9 encoding:NSASCIIStringEncoding];

View file

@ -0,0 +1 @@
str2 = str1

View file

@ -0,0 +1,2 @@
string s, t="hello"
s=t

View file

@ -0,0 +1 @@
s1=s

View file

@ -0,0 +1,2 @@
GEN string_copy = gcopy(string);
GEN string_ref = string;

View file

@ -0,0 +1,2 @@
$src = "Hello";
$dst = $src;

View file

@ -0,0 +1,3 @@
declare (s1, s2) character (20) varying;
s1 = 'now is the time';
s2 = s1;

View file

@ -0,0 +1,28 @@
program in,out;
type
pString = ^string;
var
s1,s2 : string ;
pStr : pString ;
begin
/* direct copy */
s1 := 'Now is the time for all good men to come to the aid of their party.'
s2 := s1 ;
writeln(s1);
writeln(s2);
/* By Reference */
pStr := @s1 ;
writeln(pStr^);
pStr := @s2 ;
writeln(pStr^);
end;

View file

@ -0,0 +1,6 @@
my $original = 'Hello.';
my $copy = $original;
say $copy; # prints "Hello."
$copy = 'Goodbye.';
say $copy; # prints "Goodbye."
say $original; # prints "Hello."

View file

@ -0,0 +1,6 @@
my $original = 'Hello.';
my $bound := $original;
say $bound; # prints "Hello."
$bound = 'Goodbye.';
say $bound; # prints "Goodbye."
say $original; # prints "Goodbye."

View file

@ -0,0 +1,12 @@
my $original = 'Hello.';
my $bound-ro ::= $original;
say $bound-ro; # prints "Hello."
try {
$bound-ro = 'Runtime error!';
CATCH {
say "$!"; # prints "Cannot modify readonly value"
};
};
say $bound-ro; # prints "Hello."
$original = 'Goodbye.';
say $bound-ro; # prints "Goodbye."

View file

@ -0,0 +1,4 @@
my $original = 'Hello.';
my $new = $original;
$new = 'Goodbye.';
print "$original\n"; # prints "Hello."

View file

@ -0,0 +1,4 @@
my $original = 'Hello.';
my $ref = \$original;
$$ref = 'Goodbye.';
print "$original\n"; # prints "Goodbye."

View file

@ -0,0 +1,5 @@
my $original = 'Hello.';
our $alias;
local *alias = \$original;
$alias = 'Good evening.';
print "$original\n"; # prints "Good evening."

View file

@ -0,0 +1,6 @@
use Lexical::Alias;
my $original = 'Hello.';
my $alias;
alias $alias, $original;
$alias = 'Good evening.';
print "$original\n"; # prints "Good evening."

View file

@ -0,0 +1,3 @@
(setq Str1 "abcdef")
(setq Str2 Str1) # Create a reference to that symbol
(setq Str3 (name Str1)) # Create new symbol with name "abcdef"

View file

@ -0,0 +1,4 @@
int main(){
string hi = "Hello World.";
string ih = hi;
}

View file

@ -0,0 +1,3 @@
vars src, dst;
'Hello' -> src;
copy(src) -> dst;

View file

@ -0,0 +1,2 @@
vars src='Hello';
vars dst=copy(src);

View file

@ -0,0 +1 @@
(hello) dup length string copy

View file

@ -0,0 +1,2 @@
$str = "foo"
$dup = $str

View file

@ -0,0 +1 @@
$dup = $str.Clone()

View file

@ -0,0 +1,4 @@
editvar /newvar /value=a /userinput=1 /title=Enter a string to be copied:
editvar /newvar /value=b /userinput=1 /title=Enter current directory of the string:
editvar /newvar /value=c /userinput=1 /title=Enter file to copy to:
copy -a- from -b- to -c-

View file

@ -0,0 +1,2 @@
src$ = "Hello"
dst$ = src$

View file

@ -0,0 +1,8 @@
>>> 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

View file

@ -0,0 +1,6 @@
>>> a = 'hello'
>>> b = ''.join(a)
>>> a == b
True
>>> b is a ### Might be True ... depends on "interning" implementation details!
False

View file

@ -0,0 +1,2 @@
str1 <- "abc"
str2 <- str1

View file

@ -0,0 +1,23 @@
REBOL [
Title: "String Copy"
Date: 2009-12-16
Author: oofoe
URL: http://rosettacode.org/wiki/Copy_a_string
]
x: y: "Testing."
y/2: #"X"
print ["Both variables reference same string:" mold x "," mold y]
x: "Slackeriffic!"
print ["Now reference different strings:" mold x "," mold y]
y: copy x ; String copy here!
y/3: #"X" ; Modify string.
print ["x copied to y, then modified:" mold x "," mold y]
y: copy/part x 7 ; Copy only the first part of y to x.
print ["Partial copy:" mold x "," mold y]
y: copy/part skip x 2 3
print ["Partial copy from offset:" mold x "," mold y]

View file

@ -0,0 +1,2 @@
src = "this is a string"
dst = src

View file

@ -0,0 +1,4 @@
>> s1 = "A string"
A string
>> s2 = s1
A string

View file

@ -0,0 +1,2 @@
'abc' as a
a as b

Some files were not shown because too many files have changed in this diff Show more