summaryrefslogtreecommitdiff
path: root/euler17.c
blob: f6def67fa20ae57f43388c58e6bdfe1c72cc8ea3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <ctype.h>
#include <string.h>

const char* num_en[]={
	"",
	"one", "two", "three", "four",
	"five", "six", "seven", "eight",
	"nine", "ten", "eleven", "twelve",
	"thirteen", "fourteen", "fifteen", "sixteen",
	"seventeen", "eighteen", "nineteen", "twenty",
	"thirty", "forty", "fifty", "sixty",
	"seventy", "eighty", "ninety"};

void name_num(int num,char* name)
{
	name[0]=0;
	if (num>=1000){
		strcat(name,num_en[num/1000]);
		strcat(name," thousand ");
		num %= 1000;
		if (num>0 && num<100)
			strcat(name,"and ");
	}
	if (num>=100){
		strcat(name,num_en[num/100]);
		strcat(name," hundred ");
		num %= 100;
		if (num>0){
			strcat(name,"and ");
		}
	}
	if (num>20){
		strcat(name,num_en[20-2+num/10]);
		num %= 10;
		if (num>0)
			strcat(name,"-");
		strcat(name,num_en[num%10]);
	}else{
		strcat(name,num_en[num]);
	}
}

int count_letters(char* s)
{
	char *p;
	int count=0;
	for (p=s; *p; ++p){
		if (islower(*p)){
			++count;
		}
	}
	return count;
}

int main()
{
	int n,i,len,total_len=0;
	char num_en[100];
	for (i=1;i<=1000;++i){
		name_num(i,num_en);
		len = count_letters(num_en);
		total_len += len;
		//printf("%s %d\n",num_en,len);
	}
	printf("%d\n",total_len);
	return 0;
}