Sort the letters of string in alphabetical order: Difference between revisions
Content added Content deleted
Alextretyak (talk | contribs) (Added 11l) |
Not a robot (talk | contribs) (Add Draco) |
||
Line 275: | Line 275: | ||
" .Naaccddeeeeeeffghhhiiiillmmmnnooooooooorrrstttttttuwy" |
" .Naaccddeeeeeeffghhhiiiillmmmnnooooooooorrrstttttttuwy" |
||
</pre> |
</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#}}== |
=={{header|F_Sharp|F#}}== |