Thread: Numbers To words

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2

    Exclamation Numbers To words

    hey um i kind of been trying to figure this out for awhile now...well the program is supposed to convert numbers like 123 into words such as one two three the only problem is my program writes the numbers back words such as three two one...does anyone have any ideas of what i can change or add to make these numbers come out foward in my program? im also kind of like a beginer at this so i need all the help i can get


    Code:
     
    #include <stdio.h>
    void main() {
         int temp, num, count, i, reg[5], remainder;
         char *word[11] = {"one ","two ","three ","four ","five ","six ","seven ","eight ","nine "};
                 printf("Enter Number\n");
                   scanf("%d",&num);
    
                       temp = num;
                       count = 0;
    
                   while(temp !=0)
                   {
                       remainder = temp % 10;
                       temp = temp / 10;
                       reg[count] = remainder;
                       ++count;
    				}
    
    
                      for(i=1; i<=count; ++i) {
    
                       switch(i){
                            case 1:
                                switch(reg[0]){
    
                   		     case 1:
                           		     printf("%s",word[0]);
    				     break;
                   			 case 2:
                           		     printf("%s",word[1]);
                           		     break;
                   			 case 3:
                           		     printf("%s",word[2]);
                           		     break;
    
                   			 case 4:
                           		     printf("%s",word[3]);
                           		     break;
                   			 case 5:
                           		     printf("%s",word[4]);
                           		     break;
                   		     case 6:
                           		     printf("%s",word[5]);
                           		     break;
                   			 case 7:
                           		     printf("%s",word[6]);
                           		     break;
                   			 case 8:
                           		     printf("%s",word[7]);
                                     break;
    				 	  	 case 9:
                           		     printf("%s",word[8]);
                                     break;
    
    				         default:
                           			printf("Zero");
                           			break;
                           }
                           break;
                case 2 :
                         switch(reg[1]){
    
                   		  case 1:
                           	      printf("%s",word[0]);
                                  break;
                   		  case 2:
                           	      printf("%s",word[1]);
                                  break;
                   		  case 3:
                                  printf("%s",word[2]);
                                  break;
    
                   		  case 4:
                                  printf("%s",word[3]);
                                  break;
                   		  case 5:
                           	       printf("%s",word[4]);
                                   break;
                   		  case 6:
                           	      printf("%s",word[5]);
                                  break;
                   		  case 7:
                           	      printf("%s",word[6]);
                           	      break;
                   		  case 8:
                           	      printf("%s",word[7]);
                                  break;
    			  		  case 9:
                           	      printf("%s",word[8]);
                                  break;
    
                   		  default:
                           		printf("Zero");
                           		break;
    						}
    						break;
                	case 3:
                        switch(reg[2]){
    
                   		 case 1:
                           		printf("%s",word[0]);
                           		break;
                   		 case 2:
                           		printf("%s",word[1]);
                          		break;
                   		 case 3:
                           		printf("%s",word[2]);
                           		break;
    
                  		 case 4:
                           		printf("%s",word[3]);
                           		break;
                  		 case 5:
                           		printf("%s",word[4]);
                           		break;
                 		 case 6:
                           		printf("%s",word[5]);
                           		break;
                  		 case 7:
                           		printf("%s",word[6]);
                           		break;
                  		 case 8:
                           		printf("%s",word[7]);
                           		break;
    	 	 	         case 9:
                           		printf("%s",word[8]);
                                    break;
    
                   		default:
                           		printf("Zero");
                           		break;
    			}
                            break;
                 case 4:
                     switch(reg[3]){
    
                         case 1:
                           	  printf("%s",word[0]);
                              break;
                   	     case 2:
                               printf("%s",word[1]);
                               break;
                         case 3:
                              printf("%s",word[2]);
                              break;
    
                         case 4:
                              printf("%s",word[3]);
                              break;
                         case 5:
                              printf("%s",word[4]);
                              break;
                         case 6:
                              printf("%s",word[5]);
                              break;
                         case 7:
                              printf("%s",word[6]);
                              break;
                         case 8:
                              printf("%s",word[7]);
                              break;
    		             case 9:
                              printf("%s",word[8]);
                              break;
    
                   		default:
                           		printf("Zero");
                           		break;
    
    
    				}
                               break;
    
    		case 5:
                        switch(reg[4]){
    
                   	      case 1:
                                 printf("%s",word[0]);
                                 break;
                   		 case 2:
                           		printf("%s",word[1]);
                           		break;
                   		 case 3:
                           		printf("%s",word[2]);
                           		break;
    
                   		 case 4:
                           		printf("%s",word[3]);
                           		break;
                   		 case 5:
                           		printf("%s",word[4]);
                           		break;
                   		 case 6:
                           		printf("%s",word[5]);
                           		break;
                    	 case 7:
                           		printf("%s",word[6]);
                           		break;
                     	 case 8:
                           		printf("%s",word[7]);
                           		break;
    			         case 9:
                           		printf("%s",word[8]);
                                    break;
                   		default:
                           		printf("Zero");
                           		break;
    
    
    }
    }
    }
    }
    Last edited by Sesshokotsu; 10-05-2006 at 06:03 PM.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Right now, you make it into a string as you go backwards (starting at 1's place and going up) and convert it immediately. You could convert it first, then go forward through the string. Note that blocks like this

    Code:
                                switch(reg[0]){
    
                   		     case 1:
                           		     printf("%s",word[0]);
    				     break;
                   			 case 2:
                           		     printf("%s",word[1]);
                           		     break;
                   			 case 3:
                           		     printf("%s",word[2]);
                           		     break;
    
                   			 case 4:
                           		     printf("%s",word[3]);
                           		     break;
                   			 case 5:
                           		     printf("%s",word[4]);
                           		     break;
                   		     case 6:
                           		     printf("%s",word[5]);
                           		     break;
                   			 case 7:
                           		     printf("%s",word[6]);
                           		     break;
                   			 case 8:
                           		     printf("%s",word[7]);
                                     break;
    				 	  	 case 9:
                           		     printf("%s",word[8]);
                                     break;
    
    				         default:
                           			printf("Zero");
                           			break;
    Can be reduced to

    Code:
    printf("%s", word[reg[0] - 1]);
    Or just add zero into your array and then

    Code:
    printf("%s", word[reg[0]]);

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. converting numbers to words
    By dionys in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 09:34 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM