Help with Arrays in Objective C.
Posted
by NJTechie
on Stack Overflow
See other posts from Stack Overflow
or by NJTechie
Published on 2010-05-23T17:02:00Z
Indexed on
2010/05/23
17:11 UTC
Read the original article
Hit count: 329
Problem : Take an integer as input and print out number equivalents of each number from input. I hacked my thoughts to work in this case but I know it is not an efficient solution.
For instance :
110
Should give the following o/p :
one
one
zero
Could someone throw light on effective usage of Arrays for this problem?
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int input, i=0, j,k, checkit;
int temp[i];
NSLog(@"Enter an integer :");
scanf("%d", &input);
checkit = input;
while(input > 0)
{
temp[i] = input%10;
input = input/10;
i++;
}
if(checkit != 0)
{
for(j=i-1;j>=0;j--)
{
//NSLog(@" %d", temp[j]);
k = temp[j];
//NSLog(@" %d", k);
switch (k) {
case 0:
NSLog(@"zero");
break;
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;
default:
break;
}
}
}
else
NSLog(@"zero");
[pool drain];
return 0;
}
© Stack Overflow or respective owner