Split a character string based on change of character: Difference between revisions

Add Refal
(→‎BQN: add)
(Add Refal)
 
(15 intermediate revisions by 7 users not shown)
Line 283:
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
<syntaxhighlight lang="algol68">BEGIN
# returns s with ", " added between each change of character #
Line 295 ⟶ 294:
[ 3 * ( ( UPB s - LWB s ) + 1 ) ]CHAR result;
INT r pos := LWB result;
INT s pos := LWB s;
CHAR s char := s[ LWB s ];
FOR s pos FROM LWB s TO UPB s DO
Line 317 ⟶ 315:
<pre>
g, HHH, 5, YY, ++, ///, \
</pre>
 
=={{header|Amazing Hopper}}==
VERSION 1: string
<syntaxhighlight lang="c">
#include <basico.h>
 
#define INICIO 1
#define CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\"+-/ \\:,;:_*"
 
algoritmo
objetivo = "gHHH5YY\"\"++ ///,,,\\", indice=0
largo=0, sublargo=0, v=0
 
#( largo = len(indice:=(onechar(CHARS,objetivo))) )
 
t=0, nuevo=""
para cada caracter ( v, indice, largo )
#(t = replicate(v, sublargo := ((poschar(INICIO, v, objetivo) - 1 ) ) ))
#(nuevo = cat( cat(nuevo, t), ", "))
objetivo+=sublargo
siguiente
nuevo -= 2
imprimir( "NEW STRING=\n", nuevo,NL)
 
terminar
</syntaxhighlight>
{{out}}
<pre>
$ hopper3 basica/splitrep.bas
NEW STRING=
g, HHH, 5, YY, "", ++, , ///, ,,,, \
</pre>
 
 
VERSION 2: arrays
<syntaxhighlight lang="c">
 
#include <basico.h>
 
#define INICIO 1
#define CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\"+-/ \\:,;:_*"
 
algoritmo
objetivo = "gHHH5YY\"\"++ ///,,,,\\", indice=0
largo=0, sublargo=0, lista={}, v=0
#( largo = len(indice:=(onechar(CHARS,objetivo))) )
 
para cada caracter ( v, indice, largo )
#( replicate(v, sublargo := ((poschar(INICIO, v, objetivo) - 1 ))))
meter en( lista )
objetivo+=sublargo
siguiente
imprimir( "LISTA=\n", lista, NL )
 
 
terminar
</syntaxhighlight>
{{out}}
<pre>
$ hopper3 basica/splitrep2.bas
LISTA=
g,HHH,5,YY,"",++, ,///,,,,,,\
</pre>
 
Line 759 ⟶ 824:
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">Split ← (¯1+`⊏⊸»⊸≠)⊸⊔
Join ← {∾⟜𝕨⊸∾´𝕩}
 
Line 1,134 ⟶ 1,199:
g, HHH, 5, YY, ++, ///, \
</pre>
 
=={{header|ed}}==
 
<syntaxhighlight lang="sed">
# by Artyom Bologov
H
# Repeating two times because matching is greedy
g/.*/s/((.)\2*)((.)\4*)/\1\
\3/g
g/.*/s/((.)\2*)((.)\4*)/\1\
\3/g
,p
Q
</syntaxhighlight>
 
{{out}}
 
<pre>$ cat split-on-change.ed | ed -lEGs split-on-change.input
Newline appended
g
HHH
5
YY
++
///
\
</pre>
 
=={{header|Elixir}}==
Line 1,710 ⟶ 1,802:
<pre>string: gHHH5YY++///\
separated: g, HHH, 5, YY, ++, ///, \</pre>
 
=={{header|K}}==
<syntaxhighlight lang="k">split: {(&~=':x)_x}
 
","/ split "gHHH5YY++///\\"</syntaxhighlight>
{{out}}
<pre>"g,HHH,5,YY,++,///,\\"</pre>
 
=={{header|Kotlin}}==
<syntaxhighlight lang="kotlin">// version 1.0.6
 
<syntaxhighlight lang="scala">// version 1.0.6
 
fun splitOnChange(s: String): String {
Line 1,728 ⟶ 1,826:
println(splitOnChange(s))
}</syntaxhighlight>
 
{{out}}
<pre>
Line 1,735 ⟶ 1,832:
 
=== Using fold() ===
 
<syntaxhighlight lang="kotlin">
 
Line 1,747 ⟶ 1,843:
}
</syntaxhighlight>
 
{{out}}
<pre>
Line 2,371 ⟶ 2,466:
* {MAN, ZERO WIDTH JOINER, WOMAN, ZERO WIDTH JOINER, GIRL, ZERO WIDTH JOINER, BOY} x 1
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Prout <Join (', ') <Split 'gHHH5YY++///\\'>>>;
};
 
Split {
(e.Cur) = (e.Cur);
(e.Cur s.1) s.1 e.X = <Split (e.Cur s.1 s.1) e.X>;
(e.Cur) s.1 e.X = (e.Cur) <Split (s.1) e.X>;
s.1 e.X = <Split (s.1) e.X>;
};
 
Join {
(e.Joiner) = ;
(e.Joiner) (e.Str) = e.Str;
(e.Joiner) (e.Str) e.Strs = e.Str e.Joiner <Join (e.Joiner) e.Strs>;
};</syntaxhighlight>
{{out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
=={{header|REXX}}==
===version 1===
Line 2,570 ⟶ 2,684:
g, HHH, 5, YY, ++, ///, \
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program split_a_character_string_based_on_change_of_character;
s := "gHHH5YY++///\\";
print(join_strings(", ", split_on_change(s)));
 
proc split_on_change(s);
parts := [];
loop while s /= "" do
parts with:= span(s, s(1));
end loop;
return parts;
end proc;
 
proc join_strings(s, parts);
if parts=[] then return ""; end if;
return parts(1) +/ [s + part : part in parts(2..)];
end proc;
end program;</syntaxhighlight>
{{out}}
<pre>g, HHH, 5, YY, ++, ///, \</pre>
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func group(str) {
Line 2,755 ⟶ 2,889:
Second variant:
++, ///, 5, HHH, YY, \, g
</pre>
 
=={{header|Uiua}}==
{{works with|Uiua|0.11.1}}
<syntaxhighlight lang="uiua">
&p/$"_, _"⊜□+1⊸⊛ "gHHH5YY++///\\"
</syntaxhighlight>
{{out}}
<pre>
g, HHH, 5, YY, ++, ///, \
</pre>
 
Line 2,823 ⟶ 2,967:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var split = Fn.new { |s|
if (s.count == 0) return ""
var res = []
2,126

edits