diff options
-rwxr-xr-x | reworking/wordnums | bin | 0 -> 17296 bytes | |||
-rw-r--r-- | reworking/wordnums.c | 67 |
2 files changed, 67 insertions, 0 deletions
diff --git a/reworking/wordnums b/reworking/wordnums Binary files differnew file mode 100755 index 0000000..98bbf35 --- /dev/null +++ b/reworking/wordnums diff --git a/reworking/wordnums.c b/reworking/wordnums.c new file mode 100644 index 0000000..b7c6b3f --- /dev/null +++ b/reworking/wordnums.c @@ -0,0 +1,67 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +// Problem 17 - Number Letter Counts +// So I found the string approach to be a pain in the ass in C (see Python solution) +// But then I realized, hey, I just need the lengths, so you don't have to pass the string! +const char *ones[10] = {"","one", "two", "three", "four", + "five", "six", "seven", "eight", "nine"}; +const char *teens[10] = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", + "sixteen", "seventeen", "eighteen", "nineteen"}; +const char *tens[10] = {"","", "twenty", "thirty", "forty", + "fifty", "sixty", "seventy", "eighty", "ninety"}; +const char *nd = "AND"; + + +///starting to think recursion is wrong way to go about this... +int lengthOfName(char *num){ + char *word; + // minus 1 for null terminator + int digits = strlen(num) - 1; + int n = atoi(num); + if(digits == 1){ + printf("here"); + word = (char *) ones[n]; + printf("%s\n", word); + return strlen(word); + } + if(digits == 2){ + // if the first digit, ie. the tens column is 1 + if(num[0] == '1'){ + word = (char *) teens[n % 10]; + printf("%s\n", word); + return strlen(word); + } + // assuming the first digit is not one, if the second digit is 0 + else if(num[1] == '0'){ + word = (char *) tens[n / 10]; + printf("%s\n", word); + return strlen(word); + } + else{ + char o[2]; + int on; + word = (char *) tens[n / 10]; + printf("%s", word); + o[0] = num[1]; + printf("%s\n", o); + on = lengthOfName(o); + return (strlen(word) + on); + } + } + return -1; +} + +int main(){ + // 4 is the max for this example, +1 for null terminator or if user tries to cheat + char n[5]; + int out; + printf("Number from 1 to 1000: "); + fgets(n, 10, stdin); + if(atoi(n) > 1000) + return printf("only up to 1000 in this example, sorry :(\n"); + out = lengthOfName(n); + printf("%d Characters... wow you'll hurt your hands typing that!\n", out); + return 0; +} |