/* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Author: Marco Vitetta E-mail: WebSite: http://ervito.altervista.org */ #include #include #include int count_letters(char *string); char *itow(int value); // itow() needs string.h library char NUMBERS_IN_WORDS[][11]={ // and the initialization below. "zero ", "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",}; int main() { int sum=0,value; for(value=1;value<1001;value++) sum+=count_letters(itow(value)); printf("\n\t%i\n\n",sum); return 0; } int count_letters(char *string) { int index=0,length=strlen(string),letters=0; for(;index='a' && string[index]<='z')|| (string[index]>='A' && string[index]<='Z') ) letters++; return letters; } char *itow(int value) // "Int TO Words" function { char digits[13]="000000000000",*words; int digits_number,index; words=malloc(50*sizeof(char)); if(value<0) { sprintf(words,"minus "); value=-value; } digits_number=sprintf(digits,"%i",value); if(digits_number%3!=0) // The string will be parsed in 3-digits tokens { do { for(index=digits_number+3-(digits_number%3);index-->0;) digits[index+1]=digits[index]; digits[0]='0'; }while(digits[digits_number+3-(digits_number%3)]!='\0'); digits_number+=3-(digits_number%3); } do { for(index=0;index<3;index++) //----------------//----------//----------------//---------------------------------------- if(digits[index]-48!=0) // CONVERSION // Digits // 0 1 2 ... // In the ASCII table the numeric digits { // // | // | | | | // are shifted of 48 respect their real if(index==0) // TABLE: // ASCII // 48 49 50 ... // values! { //----------------//----------//----------------//---------------------------------------- strcat(words,NUMBERS_IN_WORDS[digits[index]-48]); strcat(words,"hundred"); if(digits[1]-48!=0 || digits[2]-48!=0) strcat(words," and"); else index=3; strcat(words," "); } else { if(index!=1) strcat(words,NUMBERS_IN_WORDS[digits[index]-48]); else { if(digits[1]-48<2) strcat(words,NUMBERS_IN_WORDS[digits[++index]-38]); else strcat(words,NUMBERS_IN_WORDS[digits[1]-30]); } } } if(digits_number>9) strcat(words,"billion "); else if(digits_number>6) strcat(words,"million "); else if(digits_number>3) strcat(words,"thousand "); for(index=0;index0); return words; }