Sort the letters of string in alphabetical order: Difference between revisions
Sort the letters of string in alphabetical order (view source)
Revision as of 08:31, 31 January 2022
, 2 years agoAdd Draco
Alextretyak (talk | contribs) (Added 11l) |
Not a robot (talk | contribs) (Add Draco) |
||
Line 275:
" .Naaccddeeeeeeffghhhiiiillmmmnnooooooooorrrstttttttuwy"
</pre>
=={{header|Draco}}==
<lang draco>/* Sort a string in place, using a counting sort. */
proc nonrec stringsort(*char str) void:
[256] word counts;
byte i;
word j;
char c;
channel input text chi;
channel output text cho;
/* zero array */
for i from 0 upto 255 do counts[i] := 0 od;
/* count all characters */
open(chi, str);
while read(chi; c) do counts[c] := counts[c] + 1 od;
close(chi);
/* write the characters back in order */
open(cho, str);
for i from 0 upto 255 do
for j from 1 upto counts[i] do
write(cho; pretend(i, char))
od
od;
close(cho)
corp
/* Test */
proc nonrec main() void:
*char s;
s := "Now is the time for all good men to come to the aid of their country.";
writeln(s);
stringsort(s);
writeln(s)
corp</lang>
{{out}}
<pre>Now is the time for all good men to come to the aid of their country.
.Naaccddeeeeeeffghhhiiiillmmmnnooooooooorrrstttttttuwy</pre>
=={{header|F_Sharp|F#}}==
|