Write a function/method/subroutine/... that when given an integer greater than or equal to zero returns a string of the number followed by an apostrophe then the ordinal suffix.



Example

Returns would include 1'st 2'nd 3'rd 11'th 111'th 1001'st 1012'th



Task

Use your routine to show here the output for at least the following (inclusive) ranges of integer inputs:
0..25, 250..265, 1000..1025


Note: apostrophes are now optional to allow correct apostrophe-less English.




include "cowgol.coh";
include "strings.coh";
 
# Given an int32, return decimal string and add an ordinal suffix
sub ordsfx(n: int32, buf: [uint8]): (out: [uint8]) is
    var sfx: [uint8][] := {"th", "st", "nd", "rd"};
    out := buf;
    buf := IToA(n, 10, buf); # IToA is included in standard library
 
    # Since we now already have the digits, we can make the decision
    # without doing any more work.
    if (n > 10 and [@prev @prev buf] == '1') 
    or ([@prev buf] > '3') then
        CopyString(sfx[0], buf);
    else
        CopyString(sfx[[@prev buf]-'0'], buf);
    end if;
end sub;
 
# Print suffixed numerals from start to end inclusive
sub test(start: int32, end_: int32) is
    var buf: uint8[16]; # buffer
 
    var n: uint8 := 10;
    while start <= end_ loop
        print(ordsfx(start, &buf[0]));
        print_char(' ');
        start := start + 1;
        n := n - 1;
        if n == 0 then
            print_nl();
            n := 10;
        end if;
    end loop;
    print_nl();
end sub;
 
test(0, 25);
test(250,265);
test(1000,1025);