Converting Digits to Words Help!

This is a discussion on Converting Digits to Words Help! within the C Programming forums, part of the General Programming Boards category; Hello Everyone, My task is to convert an inputted number i.e. 234567 and print out the words of each digit ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    10

    Converting Digits to Words Help!

    Hello Everyone,

    My task is to convert an inputted number i.e. 234567 and print out the words of each digit so it would print "two three four five six seven".

    I don't even know where to start. All I know is that i need or should be using switch statements.

    Any help will be greatly appreciated. Thanks in advance!

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,057
    If you read in the number as a string str, then the digit '2' is located at str[0].

    Code:
    switch(str[0]) {
    case '1':  printf("one");
        break;
    case '2':  printf("two");
        break;
    ...
    }
    You should run it in a loop and process each digit in your str.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    10
    Okay. So I understand what you are talking about and get it, but how do I increment the string?
    Thanks.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,057
    Quote Originally Posted by McZiploc View Post
    Okay. So I understand what you are talking about and get it, but how do I increment the string?
    Thanks.
    You should probably use a for loop. For example if your str is "12345" then you could do something like

    Code:
    for (int i=0; i < 5; i++) {
       switch(str[i]) {
       ...
       }
    }

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    10
    Okay, thank you again but I have a small problem. I'm trying to put a "-" between the words but having some trouble. Here's my code

    Code:
    #include <stdio.h>
    #include <strings.h>
    
    
    int main()
    {
        char number[255];
        int n, digits, x=0, num_length;
    
    
        printf("Enter a number: ");
        scanf("%s", &number);
    
    
        num_length = strlen(number);
    
    
        for(n=0; n<=num_length; n++){
            switch(number[n]){
                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;
            }
            for(x=0; x<=n; x++){
                printf("-");
            }
        }
    
    
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,057
    Your for loop should probably be:

    for (n=0; n < num_length; n++)

    Suppose your string is 10 characters. Then the ten characters are number[0], number[1], ..., number[9]

    For printing dashes in between, you probably should add something inside your case labels.

    Code:
    case '1': printf("one");
       printf("-");
       break;

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    10
    Thank you sooo much. Thanks for your time. I finally understand this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting digits into words
    By nynicue in forum C Programming
    Replies: 12
    Last Post: 11-02-2008, 05:10 AM
  2. Converting words to *'s
    By FingerPrint in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2006, 03:40 PM
  3. converting numbers to words
    By dionys in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 09:34 AM
  4. converting numbers into words
    By Kozam in forum C Programming
    Replies: 2
    Last Post: 09-30-2003, 07:49 AM
  5. converting numbers to words
    By Zim in forum C Programming
    Replies: 16
    Last Post: 05-07-2003, 05:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21