Thread: printing in words

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    4

    Question printing in words

    hello sir
    first of all i wish every board members a happy xmas and a happy new year.my wishes to salem in advance.
    sir,iam a beginner to c program.i want to know whether there is any way that we could print in words the digit i inputted, ie if i enter 45 the o/p should be forty five and if it is 1234 it should be one thousand two hundred and thirty four, like on.
    with warm wishes and thanks in anticipation
    have a nice day.

  2. #2
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    What you would want to do is also read in the digit place. For example, if it is in the 10ths place, the that would be a certain ending. You see? I think it would just be a lot of switch or else-if statements. If is however you want to do it.

    --Garfield
    1978 Silver Anniversary Corvette

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    you might use an array of char *strings, {"Forty "},{"Fifty"}, ...
    Parse the input, into an array maybe a linked list.
    I think the basic algorithm is that each length is measured and thus assigned a "magnitude", then reverse iterated through. The "tail" end is the tens place and so on.

    123456[7]<--start here

    Reassemble input string and display in it's new format.

    what do you have so far?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    34
    Try this. It should be fairly simple to convert the remaining numbers to text.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    /*************************** FUNCTION PROTOTYPES *******************************/
    
    void NumToWord(double num);
    void ParseNum(char *strnum, int i, int level);
    void PrintIt(char *str, int level);
    
    /*******************************************************************************/
    
    int main(void)
    {
    	double num;
    
    	// test data
    	num = 12345678901.23;
    	NumToWord(num);
    
    	printf("\n");
    
    	// test data
    	num = 123456.78;
    	NumToWord(num);
    
    	printf("\n");
    
    	//test data
    	num = 1234567890123.45;
    	NumToWord(num);
    
    	return 0;
    }
    
    void
    NumToWord(double num)
    {
    	int i;
    	int level = 0;		// used to determine what string to print in "PrintIt"
    	char cents[3];
    	char strnum[21];	// working variable; make sure variable is large enough!
    
    	sprintf(strnum, "%.2f", num);	// make sure the double has only 2 decimal places
    
    	i = strlen(strnum);
    
    	do								// find the period, if it exists
    	{
    		i--;	
    	}
    	while (i && strnum[i] != '.');
    
    	if (i)							// found a period, parse out the "cents" amount
    	{
    		strncpy(cents, strnum+i+1, 2);
    		cents[2] = '\0';
    		strnum[i] = '\0';
    	}
    
    	ParseNum(strnum, i, level);		// start parsing the whole amount
    
    	PrintIt(cents, 6);
    }
    
    void
    PrintIt(char *str, int level)
    {
    	switch (level)
    	{
    	case 1:
    		printf(" %s Dollars ", str);
    		break;
    
    	case 2:
    		printf(" %s Thousand ", str);
    		break;
    
    	case 3:
    		printf(" %s Million ", str);
    		break;
    
    	case 4:
    		printf(" %s Billion ", str);
    		break;
    
    	case 5:
    		printf(" %s Trillion ", str);
    		break;
    
    	case 6:
    		printf(" and %s cents", str);
    		break;
    	}
    }
    
    void
    ParseNum(char *strnum, int i, int level)
    {
    	char str[4];
    
    	if (i >= 3)
    	{
    		str[0] = strnum[i-3];
    		str[1] = strnum[i-2];
    		str[2] = strnum[i-1];
    		str[3] = '\0';
    		strnum[i-3] = '\0';
    		i -= 3;
    
    		level++;
    
    		if (i)
    			ParseNum(strnum, i, level);	// not done, recursive call to parse again
    
    		PrintIt(str, level);
    	}
    	else if (i == 2)
    	{
    		str[0] = strnum[i-2];
    		str[1] = strnum[i-1];
    		str[2] = '\0';
    
    		level++;
    		PrintIt(str, level);
    	}
    	else if (i == 1)
    	{
    		str[0] = strnum[i-1];
    		str[1] = '\0';
    		
    		level++;
    		PrintIt(str, level);
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginners Contest #2 For those who wanted more!!
    By ILoveVectors in forum Contests Board
    Replies: 16
    Last Post: 08-12-2005, 12:03 AM
  2. Problem with malloc() and sorting words from text file
    By goron350 in forum C Programming
    Replies: 11
    Last Post: 11-30-2004, 10:01 AM
  3. Printing out 2 largest words
    By 0rion in forum C Programming
    Replies: 2
    Last Post: 06-02-2004, 12:51 AM
  4. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM