Convert Numbers to Words (Dev C++ Code)

jnbgames.dev 0 Tallied Votes 402 Views Share

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:

/*-----------------------------------------------------------
 * 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,580 "Nothing to see here." Moderator

Here's a good example where I'd turn to Rosettacode at https://rosettacode.org/wiki/Number_names
There are over 91 implementations of this task in almost all the computer languages we have today.

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,084 The Queen of DaniWeb Administrator Featured Poster Premium Member

I don't know about C++, but PHP has a built-in function that does just this.

When I follow rproffitt's link to Rosetta Code, there's this complicated NumberToEnglish() function. However, there's no need for any of this. Just use https://www.php.net/manual/en/class.numberformatter.php

<?php
$formatter = new NumberFormatter("en", NumberFormatter::SPELLOUT);
echo $formatter->format(123); // Prints out "one hundred twenty-three"    
?>

It even works in any language.

Anyone know if C++ supports something similar?

Rosetta Code is useless in cases like this.

rproffitt 2,580 "Nothing to see here." Moderator

When I have what is usually a small task/function to create I don't always turn to the web but may just write it if it's trivial or check the fine manual. Some get offended if you tell them RTFM but there's so many functions ready to use today. Sometimes I'll cherry pick something from the Linux source code, the fine manual, Rosettacode and other places since what we do don't have to be entirely created anew.

Sometimes a new coder fresh out of school being taught that all code they submit must be their out creation has a rough few days until they learn that it's OK to check out prior solutions.

And yes, I did encounter a professor that trash talked Wikipedia, Rosettacode and more. Different environment, different views.

Dani 4,084 The Queen of DaniWeb Administrator Featured Poster Premium Member

I'm always looking for shortcuts, but I'm not going to explicitly go to RosettaCode or here or there and search individual websites, one at a time. I'm going to do a Google search and see what comes up. The goal is for these specific code snippets at DaniWeb to be what comes up for people. Sometimes we are. Most of the times, we aren't.

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.