Tokenize a string: Difference between revisions

Add Cowgol
(Added output.)
(Add Cowgol)
Line 1,149:
(defun write-with-periods (strings)
(format t "~{~A~^.~}" strings))</lang>
 
=={{header|Cowgol}}==
<lang cowgol>include "cowgol.coh";
include "strings.coh";
 
# Tokenize a string. Note: the string is modified in place.
sub tokenize(sep: uint8, str: [uint8], out: [[uint8]]): (length: intptr) is
length := 0;
loop
[out] := str;
out := @next out;
length := length + 1;
while [str] != 0 and [str] != sep loop
str := @next str;
end loop;
if [str] == sep then
[str] := 0;
str := @next str;
else
break;
end if;
end loop;
end sub;
 
# The string
var string: [uint8] := "Hello,How,Are,You,Today";
 
# Make a mutable copy
var buf: uint8[64];
CopyString(string, &buf[0]);
 
# Tokenize the copy
var parts: [uint8][64];
var length := tokenize(',', &buf[0], &parts[0]) as @indexof parts;
 
# Print each string
var i: @indexof parts := 0;
while i < length loop
print(parts[i]);
print(".\n");
i := i + 1;
end loop;</lang>
{{out}}
<pre>Hello.
How.
Are.
You.
Today.</pre>
 
=={{header|D}}==
2,114

edits