The program below is written in C and is used to convert numbers to words. These operations are beneficial during the development of word processing applications. The program takes the input file that contains the number and outputs the results in an output file. Let me know if you find the code below helpful and if you have any additional suggestions:
Convert Numbers to Words (Dev C++ Code)
/*-----------------------------------------------------------
* Description: Program that converts numbers to words.
*-----------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h> // for atoi() and calloc()
#include <string.h> // for strlen()
char *tens[]={"ten", "eleven", "twelve", "thirteen", "fourteen","fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
char *ones[]={"zero","one","two","three","four","five","six","seven","eight","nine"}; // multi D Array [][] is the same as *[]
char *teen[]={"","","twenty", "thirty", "forty", "fifty","sixty", "seventy", "eighty", "ninety"};
void ten(int);
void tee(int); // Functions Prototype
void hun(int);
void read(FILE*,int *,int *); // function to read from the file
void write_display(FILE*); // function to write in the file and display to stdout (standard output)
static FILE *OUT; // declare a file pointer and make it accessible to every function so we can write flexibly
int main(void)
{
char b[10];// 9 digits and 1 for the '\0'
int size,i,*s,n=0; // size for file size // i counter // *s points to the first element of the array // n number of elements read from the file
FILE *IN=fopen("input.txt","r"); // declare a file pointer and open it in the read mode
OUT=fopen("output.txt","w"); // open the output file in the write mode
fseek(IN, 0, SEEK_END); // seek to the end of the file
size=ftell(IN); // Get the exact size of the file
fseek(IN,0,SEEK_SET); // seek back to the beginning to make the reading process possible
s=(int*)calloc(size,sizeof(int)); // Dynamic Memory Allocation
read(IN,&n,s); // read from the file "input.txt"
puts("Welcome to the number to words converter:\nOpening files...\n");
for (i=0;s[i]!=-1;i++)
{
fprintf(OUT,"Number %d: %d\n",i+1,s[i]);
itoa(s[i],b,10); // To convert integer to string "10" means decimal so we can calculate the lenght using strlen()
switch (strlen(b))
{
case 1:
fprintf(OUT,"%s", ones[s[i]]);
break;
case 2:
((s[i]/10)%10==1) ? ten(s[i]) : tee(s[i]);
break;
case 3:
hun(s[i]);
break;
case 4:
fprintf(OUT,"%s thousand ",ones[(s[i]/1000)]);
((s[i]%1000)<100) ? ((s[i]%1000)/10) ? ten((s[i]%1000)) : tee((s[i]%1000)) : hun((s[i]%1000)); // double selection
break;
case 5:
((s[i]/10000)==1) ? ten((s[i]/1000)) : tee((s[i]/1000));
fprintf(OUT,"thousand ");
hun(s[i]%1000);
break;
case 6:
hun(s[i]/1000);
fprintf(OUT,"thousand ");
(s[i]%1000!=0) ? hun(s[i]%1000) : printf("");
break;
case 7:
fprintf(OUT,"%s million",ones[s[i]/1000000]);
hun((s[i]/1000)%1000);
(((s[i]/1000)%1000)!=0) ? fprintf(OUT,"thousand ") : fprintf(OUT,"");
(s[i]%1000!=0) ? hun(s[i]%1000) : printf("") ;
break;
case 8:
(s[i]/10000000==1) ? ten(s[i]/1000000) : tee(s[i]/1000000);
fprintf(OUT,"million ");
hun((s[i]/1000)%1000);
(((s[i]/1000)%1000)!=0) ? fprintf(OUT,"thousand ") : fprintf(OUT,"");
hun(s[i]%1000);
break;
case 9:
hun(s[i]/1000000);
fprintf(OUT,"million ");
hun((s[i]/1000)%1000);
(((s[i]/1000)%1000)!=0) ? fprintf(OUT,"thousand\n") : fprintf(OUT,"");
hun(s[i]%1000);
break;
default :
puts("The number is more than 9 digits conversion is impossible.");
}
fprintf(OUT,"\n");
}
fclose(OUT); // close the output file after writing all the numbers
write_display(OUT); // display what was writeen in the "output.txt" file to stdout the screen stream
puts("-1 is encountered\nAll numbers in words have been stored in the output file\nClosing files...");
return 0;
}
void ten(int s)
{
fprintf(OUT,"%s ",tens[s%10]);
}
void tee(int s)
{
((s%10)==0) ? fprintf(OUT,"%s ",teen[(s/10)%10]): fprintf(OUT,"%s %s ",teen[(s/10)%10],ones[s%10]);
}
void hun(int s)
{
(s/100 !=0) ? fprintf(OUT,"%s hunderd ",ones[(s/100)]) : fprintf(OUT,"");
(((s%100)/10)==1) ? ten(s%100) : tee(s%100);
}
void read(FILE *IN,int *n,int *s)
{
int j; // counter
for (j=0;!feof(IN);j++) // loop until the end of file (eof)
{
fscanf(IN,"%d",&s[j]);
(*n)++; // get the number of elements stored in the file
}
fclose(IN); // close the input file
}
void write_display(FILE *OUT)
{
int j; // counter
char r[100][100];
if ((OUT=fopen("output.txt","r"))==NULL) // check if null pointer
puts("Couldn't open the file");
else
{
for (j=0;!feof(OUT);j++) // loop until the end of file "EOF"
{
fgets(r[j],100,OUT);
printf("%s ",r[j]);
puts(" "); // newline
}
fclose(OUT);
}
}
rproffitt 2,662 "Nothing to see here." Moderator
jnbgames.dev commented: I would say that mine is more detailed and general than the Rosettacode example. But good point, thank you for sharing! +0
Dani 4,329 The Queen of DaniWeb Administrator Featured Poster Premium Member
rproffitt 2,662 "Nothing to see here." Moderator
Dani 4,329 The Queen of DaniWeb Administrator Featured Poster Premium Member
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.