Anonymous user
Tokenize a string: Difference between revisions
→[[Tokenizing A String#ALGOL 68]]: Who split the beetles: substring OR chararray?
(added Io) |
(→[[Tokenizing A String#ALGOL 68]]: Who split the beetles: substring OR chararray?) |
||
Line 25:
=={{header|ALGOL 68}}==
main:(
OP +:= = (REF FLEX[]STRING list, STRING item)VOID:(▼
OP +:=
in out := new
);
PROC string split = (REF STRING
""" Split beetles where substr is found """;
FLEX[1:0]STRING out;
INT start := 1, pos;
WHILE string in string(
out +:=
start +:= pos + UPB
OD;
IF start > LWB
out +:=
FI;
out
);
printf(($g"."$, split("Hello,How,Are,You,Today",","),$l$))▼
""" Split beetles where character is found in chars """;
FLEX[1:0]STRING out;
FILE beetlesf;
STRING solo;
associate(beetlesf, beetles);
make term(beetlesf, chars);
PROC raise logical file end = (REF FILE f)BOOL: except logical file end;
on logical file end(beetlesf, raise logical file end);
DO
getf(beetlesf, ($g$, solo));
out+:=solo;
getf(beetlesf, ($x$))
OD;
except logical file end:
SKIP;
out
▲ );
STRING beetles := "John Lennon, Paul McCartney, George Harrison, Ringo Starr";
printf(($g"."$, string split(beetles, ", "),$l$));
)
Output:<pre>
John Lennon.Paul McCartney.George Harrison.Ringo Starr.
John.Lennon..Paul.McCartney..George.Harrison..Ringo.Starr.
</pre>
=={{header|C}}==
|