Jump to content

converting a number into words


Guest Adriano
 Share

Recommended Posts

Guest Adriano

I'm studying C programming with "Practical C programming 3rd ed" by Steve Oualline (O'reilly). It's a very good book, and provides a series of exercises at the end of each chapter to test your knowledge so far.

 

I'm now stumbling at chapter 8. The exercise (8-6 if you happen to have the book) asks a program to change numbers into words, e.g. 895 is "eight nine five". I can't think of an algorithm to traverse the numbers and convert them. I could take them from the input as strings (fgets...) but I run into trouble trying to compare them... I also tried dividing by ten to get the tenths, hundredths, etc.. but haven't reached a good algorithm so far.

 

Because of the position in the book, I am supposed to use only control statements, but not pointers, functions, structs or anything too complex. While I can use some library functions (maybe atoi() or something), I guess the purpose is for me to write my own.

 

Can anyone give me a pointer to something?

Link to comment
Share on other sites

does C have the char type (I'm a C++ person)? you could use a char array...

char nums[3];

then you can seperate them out using if/else if or case, and referring to the specific one in the array:

nums[X]

X being 0 - 2 (since arrays start at 0)

Link to comment
Share on other sites

Guest Adriano

Yes, C has char arrays. Actually, strings in C are only char arrays ended in

'\0'

. But this presupposes a fixed length for the numbers, doesn't it (I'm a perfectionist)? Or else a long string of zeros until the numbers, or something... I guess I have to reread the arrays section.

Link to comment
Share on other sites

Guest Adriano

Solved, with help from a friend. It'd possibly be simpler with an array, but anyway here's the code:

 

/*86numwords.c - Exercise 8-6 - Converts numbers into words */
#include <stdio.h>
#include <stdlib.h>

char line[80]; /* the line buffer. Stores the numbers as chars */
int i; /* just a for loop counter */
int main(void){
 printf("86numwords\nConverts numbers into words\n");
 while(1) {
     printf("Please write a number (or anything else to quit): ");
     fgets(line, sizeof(line),stdin);
     line[strlen(line)-1] = '\0'; /* getting rid of the \n at the end of the string */
   
     for (i = 0; i < strlen(line); ++i) {/* this for traverses through the character array that stored the number */
         switch(line[i]) { /* ... And this switch comes up with each digit as word. */
             case '0': {
                 printf("zero ");
                 break;
             }
             case '1': {
                 printf("one ");
                 break;
             }
             case '2': {
                 printf("two ");
                 break;
             }
             case '3': {
                 printf("three ");
                 break;
             }
             case '4': {
                 printf("four ");
                 break;
             }
             case '5': {
                 printf("five ");
                 break;
             }
             case '6': {
                 printf("six ");
                 break;
             }
             case '7': {
                 printf("seven ");
                 break;
             }
             case '8': {
                 printf("eight ");
                 break;
             }
             case '9': {
                 printf("nine ");
                 break;
             }
             default: { /* A quick-n-dirty way to exit*/
           printf("That's not a valid number\n");
           return(8);
             }
         }    
     }
     printf("\n"); /* we want a carriage return after the number completes. */
     system("PAUSE"); /* Good in Windows / Dev-cpp to keep the console window from closing automatically. Delete it in Linux. */
     }    
 return 0;
}

Link to comment
Share on other sites

Guest Adriano

As promised, here's the same program done with an array. Certainly more compact, though I don't know which is more efficient.

/*86numwords.c - Exercise 8-6 - Converts numbers into words */
#include <stdio.h>
#include <stdlib.h>

char line[80]; /* the line buffer. Stores the numbers as chars */
int i; /* just a for loop counter */
char numbers[10][6] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",}; /*This two dimensional array stores 10 strings of 6 chars maximum length (remember that one char space is used by the \0 termination char) */

int main(void) {
 printf("86numwords\nConverts numbers into words\n");
 while(1) { /* This program doesn't stop. Close it with Control + C */
     printf("Please write a number: ");
     fgets(line, sizeof(line),stdin);
     line[strlen(line)-1] = '\0'; /* getting rid of the \n at the end of the string */
   
     for (i = 0; i < strlen(line); ++i) { 
         /* this for loop traverses through the character array that stored the number */

         int goodline = line[i] - '0'; /* This transforms all the input from chars to ints. */
         printf("%s ", numbers[goodline]);
     }
     printf("\n"); /* we want a carriage return after the number completes. */
     system("PAUSE"); /* Good in Windows / Dev-cpp to keep the console window from closing automatically. Delete it in Linux. */
     }    
 return 0;
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...