Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Alphabetizer;
- const
- wordList = 'The,quick,brown,dog,jumped,over,lazy,foxes,dagger';
- var
- strWords, strSorted: TStringArray;
- intWords: T2DIntArray;
- i, s, h, l: Integer;
- function Explode(str, del: string): TStringArray;
- var
- i, l: Integer;
- begin
- i := 0;
- l := -1;
- repeat
- Inc(l);
- SetLength(Result, l + 1);
- i := Pos(del, str);
- if i <= 0 then
- Break;
- Result[l] := Copy(str, 1, i - 1);
- Delete(str, 1, i + Length(del) - 1);
- until false
- Result[l] := Copy(str, 1, Length(str));
- end;
- begin
- Writeln('Sorting out "' + wordList + '" into alphabetical order!');
- strWords := Explode(Lowercase(wordList), ',');
- SetArrayLength(intWords, High(strWords) + 1);
- for i := 0 to High(strWords) do
- begin
- SetArrayLength(intWords[i], Length(strWords[i]));
- for s := 0 to High(intWords[i]) do
- intWords[i][s] := Ord(strWords[i][s + 1]);
- end;
- while High(strSorted) < High(strWords) do
- begin
- h := 0;
- for i := 1 to High(intWords) do
- begin
- l := High(intWords[h]);
- if l > High(intWords[i]) then
- l := High(intWords[i]);
- for s := 0 to l do
- if intWords[i][s] < intWords[h][s] then
- begin
- h := i;
- Break;
- end
- else if intWords[i][s] > intWords[h][s] then
- Break;
- end;
- l := High(strSorted) + 1;
- SetArrayLength(strSorted, l + 1);
- for s := 0 to High(intWords[h]) do
- strSorted[l] := strSorted[l] + Chr(intWords[h][s]);
- Swap(intWords[h], intWords[High(intWords)]);
- SetArrayLength(intWords, High(intWords));
- end;
- for i := 0 to High(strSorted) do
- Writeln(strSorted[i]);
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement