/*
Author - Nisar Ahmed
Description: Converts a number to word form.
Input - Number in digits
Output - Number in words
*/
#include< stdio.h>
#include< conio.h>
#include< stdlib.h>
#include< string.h>
char word[100]="";
char *oneDigit[10] = {"","One ","Two ","Three ","Four ","Five ","Six ","Seven ","Eight ","Nine "};
char *teenNumbers[10] = {"Ten ","Eleven ","Tweleve ","Thirteen ","Fourteen ","Fifteen ","Sixteen ","Seventeen ","Eighteen ","Nineteen "};
char *twoDigit[10] = {"","","Twenty ","Thirty ","Fourty ","Fifty ","Sixty ","Seventy ","Eighty ","Ninety "};
int check(char *c);
char * cutter(char *c,int start);
char * subString(char *str,int start,int end);
char *Toword799(char *c);
void shownum(char *p);
void main(){
char *num,ch;
int i;
textbackground(RED);
textcolor(WHITE);
clrscr();
do{
clrscr();
window(1,1,80,25);
gotoxy(20,8);
printf("#########################################");
gotoxy(20,9);
printf("#### 799 Web Nixar's Creation ####");
gotoxy(20,10);
printf("####---------------------------------####");
gotoxy(20,11);
printf("#### Number to Word Converter ####");
gotoxy(20,12);
printf("#########################################");
gotoxy(20,13);
printf("## ##");
gotoxy(20,14);
printf("## 1 => Enter Number ##");
gotoxy(20,15);
printf("## 2 => Exit ##");
gotoxy(20,16);
printf("## ##");
gotoxy(20,17);
printf("## Enter Choice ##");
gotoxy(43,17);
scanf("%c",&ch);
switch(ch){
case '1': gotoxy(20,18);
printf("## Enter Number ##");
gotoxy(43,18);
scanf("%s",num);
if(check(num)==0){
shownum(Toword799(num));
strcpy(word,"");
}else{
gotoxy(27,19);
printf("Please ensure that you entered\n");
gotoxy(27,20);
printf("a number between 0-999999999!!!");
getch();
}
break;
case '2':exit(0);
break;
default:main();
}
}while(1);
getch();
}
// Functio for the validation
int check(char *c){
int flag=0,i;
if(strlen(c) > 9){
return 1;
}
for(i=0;i< strlen(c);i++){
if(!(c[i]> = '0' && c[i] < ='9'))
flag=1;
}
return flag;
}
// Return sub string from given start Index to the end of string.
char * cutter(char *c,int start){
char *p;
int i;
for(i=0;i< strlen(c)-start;i++)
p[i]=c[i+start];
p[i]='\0';
return p;
}
// Return sub string from given start index to end Index.
char * subString(char *str, int start,int end){
char *substr;
int i,j;
for(j=0,i=start;i < end;i++,j++)
substr[j] = str[i];
substr[j] = '\0';
return substr;
}
// Recursive Method to create word conversion of the given number.
char* Toword799(char *num){
if(strlen(num)==9){
if(num[0]=='1'){
char *numberInString = subString(num,1,2);
int indexOfDigit = atoi(numberInString);
strcat(word,teenNumbers[indexOfDigit]);
}else{
char *numberInString = subString(num,0,1);
int indexOfDigit = atoi(numberInString);
strcat(word,twoDigit[indexOfDigit]);
numberInString = subString(num,1,2);
indexOfDigit = atoi(numberInString);
strcat(word,oneDigit[indexOfDigit]);
}
if(num[0]=='0' && num[1]=='0'){
}else{
strcat(word,"Crore ");
}
num = cutter(num,2);
Toword799(num);
}else if(strlen(num)==8){
char *numberInString = subString(num,0,1);
int indexOfDigit = atoi(numberInString);
strcat(word,oneDigit[indexOfDigit]);
if(num[0]=='0'){
}else{
strcat(word,"Crore ");
}
num = cutter(num,1);
Toword799(num);
}else if(strlen(num)==7){
if(num[0]=='1'){
char *numberInString = subString(num,1,2);
int indexOfDigit = atoi(numberInString);
strcat(word,teenNumbers[indexOfDigit]);
}else{
char *numberInString = subString(num,0,1);
int indexOfDigit = atoi(numberInString);
strcat(word,twoDigit[indexOfDigit]);
numberInString = subString(num,1,2);
indexOfDigit = atoi(numberInString);
strcat(word,oneDigit[indexOfDigit]);
}
if(num[0]=='0' && num[1]=='0'){
}else{
strcat(word,"Lacs ");
}
num = cutter(num,2);
Toword799(num);
}else if(strlen(num)==6){
char *numberInString = subString(num,0,1);
int indexOfDigit = atoi(numberInString);
strcat(word,oneDigit[indexOfDigit]);
if(num[0]=='0'){
}else{
strcat(word,"Lacs ");
}
num = cutter(num,1);
Toword799(num);
}else if(strlen(num)==5){
if(num[0]=='1'){
char *numberInString = subString(num,1,2);
int indexOfDigit = atoi(numberInString);
strcat(word,teenNumbers[indexOfDigit]);
}else{
char *numberInString = subString(num,0,1);
int indexOfDigit = atoi(numberInString);
strcat(word,twoDigit[indexOfDigit]);
numberInString = subString(num,1,2);
indexOfDigit = atoi(numberInString);
strcat(word,oneDigit[indexOfDigit]);
}
if(num[0]=='0' && num[1]=='0'){
}else{
strcat(word,"Thousand ");
}
num = cutter(num,2);
Toword799(num);
}else if(strlen(num)==4){
char *numberInString = subString(num,0,1);
int indexOfDigit = atoi(numberInString);
strcat(word,oneDigit[indexOfDigit]);
if(num[0]=='0'){
}else{
strcat(word,"Thousand ");
}
num = cutter(num,1);
Toword799(num);
}else if(strlen(num)==3){
char *numberInString = subString(num,0,1);
int indexOfDigit = atoi(numberInString);
strcat(word,oneDigit[indexOfDigit]);
if(num[0]=='0'){
}else{
strcat(word,"Hundred ");
}
num = cutter(num,1);
Toword799(num);
}else if(strlen(num)==2){
if(num[0]=='1'){
char *numberInString = subString(num,1,2);
int indexOfDigit = atoi(numberInString);
strcat(word,teenNumbers[indexOfDigit]);
}else{
char *numberInString = subString(num,0,1);
int indexOfDigit = atoi(numberInString);
strcat(word,twoDigit[indexOfDigit]);
numberInString = subString(num,1,2);
indexOfDigit = atoi(numberInString);
strcat(word,oneDigit[indexOfDigit]);
}
num = cutter(num,2);
Toword799(num);
}else if(strlen(num)==1){
char *numberInString = subString(num,0,1);
int indexOfDigit = atoi(numberInString);
strcat(word,oneDigit[indexOfDigit]);
num = cutter(num,1);
Toword799(num);
}else{
if(strcmp(word,"")==0){
strcat(word,"Zero");
}else{
return word;
}
}
return word;
}
// Function to show the converted word of the number
void shownum(char *p){
textbackground(BLUE);
textcolor(WHITE);
clrscr();
window(10,13,70,16);
cprintf("%s",p);
getch();
}