Sort the letters of string in alphabetical order: Difference between revisions
Sort the letters of string in alphabetical order (view source)
Revision as of 13:09, 13 August 2022
, 1 year agoinsert →Pascal: , allegedly add new external links [spam detection false positive]
m (→{{header|J}}) |
(insert →Pascal: , allegedly add new external links [spam detection false positive]) |
||
Line 679:
<pre> ,Taaabcdeeeefghhijkllmnnoooopppqrrrsttuuvwxyyz
.Naaccddeeeeeeffghhhiiiillmmmnnooooooooorrrstttttttuwy</pre>
=={{header|Pascal}}==
{{works with|Extended Pascal}}
In Pascal it is guaranteed that all letters of the Modern English alphabet, <tt>'A'</tt> through <tt>'Z'</tt> and <tt>'a'</tt> through <tt>'z'</tt>, are in alphabetical order.
That means <tt>ord('A') < ord('B')</tt>.
However, uppercase and lowercase letters may be separate (like in ASCII), fully or partially interspersed.
The output of this program may differ in that regard.
<lang pascal>program sortTheLettersOfStringInAlphabeticalOrder(input, output);
type
line = string(80);
{
sort characters in a string
\param sample the string to sort
\return \param sample so for all n < m: sample[n] <= sample[m]
}
function alphabeticallySorted(sample: line): line;
var
c: char;
{ `sample.capacity` refers to 80 in this program. }
i: 0..sample.capacity;
{ `… value [otherwise 0]` is an initial state specification. }
tab: array[char] of 0..sample.capacity value [otherwise 0];
begin
{ analyze: how many occurrences of every character? }
for i := 1 to length(sample) do
begin
tab[sample[i]] := tab[sample[i]] + 1
end;
{ process: rebuild string but in alphabetical order }
sample := '';
for c := chr(0) to maxChar do
begin
for i := 1 to tab[c] do
begin
sample := sample + c
end
end;
{ finish: set result variable }
alphabeticallySorted := sample
end;
{ === MAIN =================================================== }
var
s: line;
begin
while not EOF do
begin
readLn(s);
writeLn(alphabeticallySorted(s))
end
end.</lang>
=={{header|Perl}}==
|