Converting Digits to Words

Discussion in 'iPhone/iPad Programming' started by Hexiii, Mar 3, 2012.

  1. Hexiii, Mar 3, 2012
    Last edited: Mar 3, 2012

    macrumors 6502a

    Hexiii

    Joined:
    Jun 30, 2011
    Location:
    Prague, Czech Republic
    #1
    I am reading the Stephen G. Kochan book about obj-c and in the end of every chapter, there are few exercises to train what have you learned so far.

    I am having a little problem with one exercise where I am supposed to write a small program to convert digits to words. (input: 34524, output: three four five two four) I tried searching google but there are just examples for rewriting number to text (1423, one thousand four hundred twenty three).

    I only figured out how to rewrite it backwards like (4213, three one two four), but can't figure out how to save the digits from the front to back.

    Here's the backwards program:

    Code:
    #import <Foundation/Foundation.h>
    
    int main (int argc, char *argv [])
    
    {
        @autoreleasepool {
                 
            int number, enddigit;
            
            NSLog(@"Type a number");
            
            scanf("%i", &number);
            
            for (; number % 10 != 0; number /= 10){
                enddigit = number % 10;
                switch (enddigit){
                    case (1):
                        NSLog(@"one");
                        break;
                        
                    case (2):
                        NSLog(@"two");
                        break;
                        
                    case (3):
                        NSLog(@"three");
                        break;
                        
                    case (4):
                        NSLog(@"four");
                        break;
                        
                    case (5):
                        NSLog(@"five");
                        break;
                        
                    case (6):
                        NSLog(@"six");
                        break;
                        
                    case (7):
                        NSLog(@"seven");
                        break;
                        
                    case (8):
                        NSLog(@"eight");
                        break;
                        
                    case (9):
                        NSLog(@"nine");
                        break;
                        
                    case (0):
                        NSLog(@"zero");
                    break;
                }
                }
        }
        
        return 0;
        
    }
    
    Thank you for help.
     
  2. macrumors 6502a

    itickings

    Joined:
    Apr 14, 2007
    #2
    I don't have the particular book you are using, but I'd guess that the question is from an early part of the book.
    If so, you probably shouldn't try to find a way to save the words after converting them from digits, but rather find a way to convert the most significant digit first, and so on.

    As for the code as it is now, what happens when you input for example 10 or 100? Or any number containing a zero?
     
  3. macrumors member

    Joined:
    Feb 25, 2012
    Location:
    Europe
    #3
    Kochan has a special forum dedicated to his books:

    http://classroomm.com/objective-c/

    there you can check how others solve the exercises and discuss other fun stuff.
     
  4. thread starter macrumors 6502a

    Hexiii

    Joined:
    Jun 30, 2011
    Location:
    Prague, Czech Republic
    #4
    I know, but I am reading translated book, so the chapters are not the same and I couldn't find the one on that forum.



    I will fix that later, I just wanted to show you the general principle.
     
  5. macrumors member

    Joined:
    Feb 25, 2012
    Location:
    Europe
    #5
    Just a friendly tip from another non-english native European, read programming books in English. It makes more sense as most (all?) languages are based on English.

    And the excercises should be the same? :confused:
     
  6. thread starter macrumors 6502a

    Hexiii

    Joined:
    Jun 30, 2011
    Location:
    Prague, Czech Republic
    #6
    Apparently, the are not.
     

Share This Page