Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Hexiii

macrumors 65816
Original poster
Jun 30, 2011
1,113
373
Prague, Czech Republic
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.
 
Last edited:
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.

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?
 
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.

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.


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


I will fix that later, I just wanted to show you the general principle.
 
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.

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:
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.