You seem to be double-counting some numbers.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HUNDRED_AND 10
#define HUNDREDS 7
#define THOUSAND 11
#define LIMIT 1000
int units(int units);
int tens(int tens);
int ten_to_nineteen(int units);
int count_letters(int number);
int main()
{
int i;
long long unsigned int letter_count = 0;
for (i = 101; i < 120; i += 1)
{
printf("N=%d, str=", i);
letter_count += count_letters(i);
printf(", total so far=%llu\n", letter_count);
}
//letter_count += THOUSAND; //and the letters for 1000 onto the total
printf("number of letters in all the numbers between 1 and 1000 inclusive are %llu", letter_count);
return 0;
}
int units(int units)
{
char *single[] = {
"", "one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine"};
printf("%s", single[units]);
return strlen(single[units]);
}
int tens(int tens)
{
char *multiples_ten[] = {
"",
"",
"twenty",
"thirty",
"fourty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninety"};
printf("%s", multiples_ten[tens]);
return strlen(multiples_ten[tens]);
}
int ten_to_nineteen(int units)
{
char *teens[] = {
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen"};
printf("%s", teens[units]);
return strlen(teens[units]);
}
int count_letters(int number)
{
int unit, ten, hundred, total_letters_in_num = 0;
unit = number % 10;
ten = number % 100;
ten /= 10;
hundred = number / 100;
if (unit > 0)
{
total_letters_in_num = units(unit); //count the number of letters in the number of units.
}
if (ten > 0)
{
if (ten == 1)
{
total_letters_in_num = 0;
total_letters_in_num += ten_to_nineteen(unit);
}
else
{
total_letters_in_num += tens((ten));//count the number of letters in the number of groups of 10
}
}
if (hundred > 0)
{
total_letters_in_num += units(hundred);
if (unit == 0 && ten == 0) //dealing with 100 200, 300 etc
{
printf("hundred");
total_letters_in_num += HUNDREDS;
}
else
{
printf("hundredand");
total_letters_in_num += HUNDRED_AND;
}
}
return total_letters_in_num;
}
Results
Code:
$ ./a.out
N=101, str=oneonehundredand, total so far=16
N=102, str=twoonehundredand, total so far=32
N=103, str=threeonehundredand, total so far=50
N=104, str=fouronehundredand, total so far=67
N=105, str=fiveonehundredand, total so far=84
N=106, str=sixonehundredand, total so far=100
N=107, str=sevenonehundredand, total so far=118
N=108, str=eightonehundredand, total so far=136
N=109, str=nineonehundredand, total so far=153
N=110, str=tenonehundredand, total so far=169
N=111, str=oneelevenonehundredand, total so far=188
N=112, str=twotwelveonehundredand, total so far=207
N=113, str=threethirteenonehundredand, total so far=228
N=114, str=fourfourteenonehundredand, total so far=249
N=115, str=fivefifteenonehundredand, total so far=269
N=116, str=sixsixteenonehundredand, total so far=289
N=117, str=sevenseventeenonehundredand, total so far=311
N=118, str=eighteighteenonehundredand, total so far=332
N=119, str=ninenineteenonehundredand, total so far=353
number of letters in all the numbers between 1 and 1000 inclusive are 353
For example, all the teens also output (and thus count) the units as well.