Here is a variation on using an array of ordinals and then a simply if
and switch
to handle all values (you can adjust to handle numbers > 100).
The routine is fairly straight forward. For everything (except values 10-13) follow the normal ordinal rules. So you simply set up a if
block to handle the oddball values and then a switch
with a mod
of the value for the remainder, e.g.
Edit: - per request, you can add a check at the top of get_ordinal
to scale values greater than 100
to their equivalent 0-99
range by successively subtracting 100
(and you can add more checks to optimize values greater than 1000, etc.), e.g.
#include <stdio.h>
char *get_ordinal (char **ordinals, int value)
{
value %= 100; /* normalize values between 0-100 */
if (3 < value && value < 21)
return ordinals[3];
switch (value % 10) {
case 1 : return ordinals[0];
break;
case 2 : return ordinals[1];
break;
case 3 : return ordinals[2];
break;
default: return ordinals[3];
break;
}
}
int main (void) {
char *ordinals[] = { "st", "nd", "rd", "th" };
for (int i = 1; i < 30; i++)
printf ("Please enter the %d%s value:\n",
i, get_ordinal (ordinals, i));
return 0;
}
Example Use/Output
$ ./bin/getordinals
Please enter the 1st value:
Please enter the 2nd value:
Please enter the 3rd value:
Please enter the 4th value:
Please enter the 5th value:
Please enter the 6th value:
Please enter the 7th value:
Please enter the 8th value:
Please enter the 9th value:
Please enter the 10th value:
Please enter the 11th value:
Please enter the 12th value:
Please enter the 13th value:
Please enter the 14th value:
Please enter the 15th value:
Please enter the 16th value:
Please enter the 17th value:
Please enter the 18th value:
Please enter the 19th value:
Please enter the 20th value:
Please enter the 21st value:
Please enter the 22nd value:
Please enter the 23rd value:
Please enter the 24th value:
Please enter the 25th value:
Please enter the 26th value:
Please enter the 27th value:
Please enter the 28th value:
Please enter the 29th value:
For values greater than 100, e.g.
$ ./bin/getordinals
Please enter the 90th value:
Please enter the 91st value:
Please enter the 92nd value:
Please enter the 93rd value:
Please enter the 94th value:
Please enter the 95th value:
Please enter the 96th value:
Please enter the 97th value:
Please enter the 98th value:
Please enter the 99th value:
Please enter the 100th value:
Please enter the 101st value:
Please enter the 102nd value:
Please enter the 103rd value:
Please enter the 104th value:
Please enter the 105th value:
Please enter the 106th value:
Please enter the 107th value:
Please enter the 108th value:
Please enter the 109th value:
Please enter the 110th value:
Please enter the 111th value:
Please enter the 112th value:
Please enter the 113th value:
Please enter the 114th value:
Please enter the 115th value:
Please enter the 116th value:
Please enter the 117th value:
Please enter the 118th value:
Please enter the 119th value:
Please enter the 120th value:
Please enter the 121st value:
Please enter the 122nd value:
Please enter the 123rd value:
Please enter the 124th value:
Please enter the 125th value:
Please enter the 126th value:
Please enter the 127th value:
Please enter the 128th value:
Please enter the 129th value: