Solved

how to covert a numeric to a character string

Posted on 2002-06-08
9
283 Views
Last Modified: 2010-04-15
i been doing a project about a user menu..
i wonder how to convert a numeric to a character string
er...sorry about that, wat i really want to ask is for example if i key in a numeric number 3 then the
output will be a character "three" anf if i key in 4 it will return a character string "four". i wondder
what string library should used..
0
Comment
Question by:ChristopherNoel
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 12

Expert Comment

by:pjknibbs
ID: 7064550
Not sure entirely what you mean. If you're asking how you can convert a number stored in an integer (e.g. 25343) to a printable string, then you're looking at sprintf() or _ltoa (the latter might possibly be compiler-specific, I'm not sure). If this ISN'T what you're asking I think you need to clarify the question.
0
 

Author Comment

by:ChristopherNoel
ID: 7066919
er...sorry about that, wat i really want to ask is for example if i key in a numeric number 3 then the output will be a character "three" anf if i key in 4 it will return a character string "four". i wondder what string library should used..
0
 
LVL 4

Expert Comment

by:pagladasu
ID: 7067406
What is the range of numbers?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:AlexNek
ID: 7067702
2 ChristopherNoel
What is the big problem. Let's create an arrays with text description and parse the number in the correct range.
{"zero","one","two",..,"nine"};
{"eleven", "twelve", "thirteen",..., "nineteen"}
{"ten","twenty",..."ninety"}
{"hundred", "thousand", "million"}
I think it is standard task for students.
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 7067793
0
 
LVL 12

Expert Comment

by:pjknibbs
ID: 7069214
Well, there isn't a standard library function to do what you want, so you'll have to write your own. AlexNek has pointed you along the right path, I suspect.
0
 

Accepted Solution

by:
Vasudeesha earned 50 total points
ID: 7090727
Hi ChristopherNoel Here is C Progarm which will convert Number to Text.


**********************************************************
# include <ctype.h>
# include <stdio.h>

char *ot[3][9] = {
      { " One", " Two", " Three", " Four", " Five",
        " Six", " Seven", " Eight", " Nine" },
       { " Ten", " Twenty", " Thirty", " Forty", " Fifty",
          " Sixty", " Seventy", " Eighty", " Ninety" },
         { " Eleven", " Twelve", " Thirteen", " Fourteen",
        " Fifteen"," Sixteen"," Seventeen"," Eighteen", " Nineteen"}
         };

char *a[5] = { " Hundred", " Thousand", " Lakhs", " Crore", " Arab" } ;
char result[250] = "" ;
char *t[50] ;

main( )
{
     int i, j, ind = 0, c, r, pr = -1, e = 0 ;
     unsigned long n ;
     unsigned long q ;

     clrscr( ) ;
     printf ( "\nEnter a long unsigned number : " ) ;
     scanf ( "%ld", &n ) ;
     printf ( "%ld\n", n ) ;
     q = n ;
     if ( n == 0 )
          strcpy ( result, "Zero" ) ;
     else
     {
          for( i = 0 ; q > 0 ; i++ )
          {
     if( ( i % 2 ) && i > 2 )
     e++ ;
     r = q % 10 ;
     q /= 10 ;
     if ( r != 0 )
     {
     if ( i == 0 )
     t[++ind] = ot[i][r-1];
     else if ( i == 1 )
     {
     if ( r == 1 && pr == 0 )
     t[++ind] = ot[i][r-1] ;
     else if ( r == 1 && pr > 0 )
     t[--ind] = ot[2][r-1] ;
     else
     t[++ind] = ot[i][r-1] ;
     }
     else if ( i >= 2 )
     {
     if ( i == 2 )
     {
      t[++ind] = a[e] ;
      t[++ind] = ot[0][r-1] ;
     }
     else
     {
     if ( i % 2 )
     {
      t[++ind] = a[e] ;
      t[++ind] = ot[0][r-1] ;
     }
     else
     {
     if ( pr == 0 )
     {
      t[++ind] = a[e] ;
      t[++ind] = ot[1][r-1] ;
     }
     else if ( r == 1)
     t[ind] = ot[2][pr-1] ;
     else
     t[++ind] = ot[1][r-1] ;
     }
     }
     }
     }
     pr = r ;
     }
     }/* End for */

     for ( i = ind ; i >= 0 ; i-- )
          strcat ( result, t[i] ) ;

     printf( "\n%s", result ) ;

} /* End main */
0
 

Expert Comment

by:Vasudeesha
ID: 7090765
Hi ChristopherNoel

Please convert the above program...
....
....
....
char *a[5] = { " Hundred", " Thousand", " Lakhs", " Crore", " Arab" } ;

      ----------To---------

char *a[5] = { " Hundred", " Thousand", " Million", " Million", " Trillion" } ;

The above Numbering system we use in India... You can change to your country's format ..


Bye
Vasudeesha
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 9480224
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:
Accept Vasudeesha's comment as answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Kent (Kdo)
EE Cleanup Volunteer
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Certified Pentester Exam - need assistance on compiling an exploit 6 418
First character of input string truncated with scanf 3 105
why "." vs "->" 23 120
Unable to start eclipse ? 17 162
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question