I'm modifying some driver software for my keyboard and part of it is a plugin that outputs the date to my keyboard screen. At the moment it says 1 January but I really want it to say 1st, 2nd, 3rd or 4th or whatever.
I've been looking everywhere for some kind of code that will give me some kind of an idea on how to do it but I can only find examples for C# and I'm using C.
Edit:
const char *ordinals[] = {"", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th", "21st", "22nd", "23rd", "24th", "25th", "26th", "27th", "28th", "29th", "30th", "31st"};
sprintf(date, "%s %s", ordinals[t->tm_mday], mon);
Normally a “th” appears at the end of the number. For example, four → fourth (or 4 → 4th) and two → second (or 2 → 2nd). As you've seen before, in written English you may write a normal (cardinal) number without the “th” or “st” etc.
When writing ordinal numbers such as 1st, 2nd, 3rd, etc. you should use the last two letters on the word as it would be if you wrote out the whole word. Below are the ordinal numbers both written out and with digits for 1-20. As you can see, 1st, 2nd, and 3rd use -st, -nd, and -rd, but 4th-20th use -th.
1st, 2nd, 3rd, 4th, 5th, 6th, then use th until 21st, 22nd, 23rd, 24th, etc. Remember we always capitalise months, whether we write them in full or abbreviate them. Some style guides may add a full stop/period after the abbreviation, but it is generally OK not to use one.
You add -th to numbers written in figures and ending in 4, 5, 6, 7, 8, 9, 10, 11, 12, or 13 in order to form ordinal numbers. These numbers are pronounced as if they were written as words. For example, 7th is pronounced the same as ' seventh', and 5th is pronounced the same as ' fifth'.
Since you need this only for numbers 1
through 31
, the easiest approach is to define an array of ordinals, like this:
const char *ordinals[] = {"", "1st", "2nd", "3rd", "4th"..., "31st"};
...
printf("%s of %s", ordinals[dayNumber], monthName);
This is better than doing it algorithmically, because it is more readable, and is easier to internationalize, should you run into this at some point later on.
This works for all nonnegative n
:
char *suffix(int n)
{
switch (n % 100) {
case 11: case 12: case 13: return "th";
default: switch (n % 10) {
case 1: return "st";
case 2: return "nd";
case 3: return "rd";
default: return "th";
}
}
}
printf("%d%s\n", n, suffix(n));
void day_to_string(int day, char *buffer)
{
char *suff = "th";
switch(day)
{
case 1:
case 21:
case 31:
suff = "st";
break;
case 2:
case 22:
suff = "nd";
break;
case 3:
case 23:
suff = "rd";
break;
}
sprintf(buffer, "%d%s", day, suff);
}
Should do it. Note however that if you want to ever translate your program to another language, you may need to follow dasblinkenlight's suggestion, as you may find that the rules in some languages isn't the same as in English.
You can do it with a condition.
#include <stdio.h>
const char *suff;
switch (day)
{
case 1: /* fall-through */
case 21: /* fall-through */
case 31:
suff = "st";
break;
case 2: /* fall-through */
case 22:
suff = "nd";
break;
case 3: /* fall-through */
case 23:
suff = "rd";
break;
default:
suff = "th";
break;
}
printf("%d%s\n", day, suff);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With