Last active
August 29, 2016 15:39
-
-
Save willeccles/6abb69959343d2fa9b8bbdc82dd279a0 to your computer and use it in GitHub Desktop.
solution (including all bonuses) to https://www.reddit.com/r/dailyprogrammer/comments/4jom3a/20160516_challenge_267_easy_all_the_places_your/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
int place = 1; | |
int places = 100; | |
int main(int argc, char* argv[]) { | |
if (argc >= 3) { | |
places = atoi(argv[2]); | |
place = atoi(argv[1]); | |
} | |
else if (argc == 2) | |
place = atoi(argv[1]); | |
for (int i = 1; i <= places; i++) { | |
char* num = malloc(sizeof(char)*100); | |
sprintf(num, "%d", i); | |
char* suf = malloc(sizeof(char)*100); | |
if ((i >= 11 && i <= 13) || | |
(i > 100 && ( | |
(i-11)%100==0 || | |
(i-12)%100==0 || | |
(i-13)%100==0 ))) suf = "th"; | |
else switch(num[strlen(num)-1]) { | |
case '1': | |
suf = "st"; | |
break; | |
case '2': | |
suf = "nd"; | |
break; | |
case '3': | |
suf = "rd"; | |
break; | |
default: | |
suf = "th"; | |
break; | |
} | |
if (i != place) printf("%d%s ", i, suf); | |
} | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment