Reputation: 81
I have been learning Objective-C with the Kochan book and I can't figure out how to do this exercise program. Only odd numbered exercises are listed online and this one is even. The exercise is to convert numbers into words. So, if "932" was entered, the program should return: "nine three two"
I used a do
, while
loop but the words came out backwards, as in "two three nine". Can anyone suggest a technique that works for this?
int number, digit;
NSLog(@"Type in your integer.");
scanf("%i", &number);
do
{
digit = number % 10;
if (digit == 0)
NSLog(@"zero");
if (digit == 1)
NSLog(@"one");
if (digit == 2)
NSLog(@"two");
if (digit == 3)
NSLog(@"three");
if (digit == 4)
NSLog(@"four");
if (digit == 5)
NSLog(@"five");
if (digit == 6)
NSLog(@"six");
if (digit == 7)
NSLog(@"seven");
if (digit == 8)
NSLog(@"eight");
if (digit == 9)
NSLog(@"nine");
number /= 10;
}
while (number != 0);
Upvotes: 8
Views: 4041
Reputation: 675
very easy, there are number of approaches but i normally try this :
do
{
digit = number % 10;
switch (digit) {
case 0:
[self prependNumber:@"zero"];
break;
case 1:
[self prependNumber:@"one"];
break;
case 2:
[self prependNumber:@"two"];
break;
case 3:
[self prependNumber:@"three"];
break;
case 4:
[self prependNumber:@"four"];
break;
case 5:
[self prependNumber:@"five"];
break;
case 6:
[self prependNumber:@"six"];
break;
case 7:
[self prependNumber:@"seven"];
break;
case 8:
[self prependNumber:@"eight"];
break;
case 9:
[self prependNumber:@"nine"];
break;
default:
break;
}
number /= 10;
}
while (number != 0);
/************/
-(void) prependNumber:(NSString*)str{
NSLog(str);
}
Upvotes: 0
Reputation: 1
I use nested under nested loop but believe that this works
int i, j, number, reversenumber = 0;
NSLog(@" Input Number:");
scanf( "%i", &number);
if (number != 0)
// chekcing for zero entry
{
for (;number!= 0; number = number/10)
//for reversing the number entered so that the words doesn't come reversed when printed
{
i = number%10;
reversenumber = reversenumber * 10 + i;
}
NSLog(@"Reverser Number for the input number is %i", reversenumber);
// mid routine check to print the reversed number
while(reversenumber != 0)
{
j = reversenumber % 10;
switch (j)
{
case 9:
NSLog(@"nine");
break;
case 8:
NSLog(@"eight");
break;
case 7:
NSLog(@"seven");
break;
case 6:
NSLog(@"six");
break;
case 5:
NSLog(@"five");
break;
case 4:
NSLog(@"four");
break;
case 3:
NSLog(@"three");
break;
case 2:
NSLog(@"two");
break;
case 1:
NSLog(@"one");
break;
default:
NSLog(@"zero");
}
reversenumber /= 10;
}
}
else
NSLog(@"Zero");
}
return 0;
}
Upvotes: 0
Reputation: 31
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
// insert code here...
int number; //store the value the user enter
int lastDigit; //pick of the last digit of the integer
int tempNum; //a temporary storage of the integer the user enter
int count = 0; //used to count how many digits were entered
int count2; //going to be use as a duplicate of count
NSLog(@"Enter an integer");
scanf("%i", &number);
tempNum = number;
//Loop to find out how many digits were entered
for (; number != 0; number /= 10) {
count +=1;
}
//Loop to convert the numbers into words
for (; count != 0; count -= 1) {
count2 = count; //set count2 to count so the for and while loop use them independently
number = tempNum; //restore the value entered by by the user to the number variable
//Loop to reverse the order of the last digit
while (count2 != 0) { //loops to the same number of counts to get the first digit
lastDigit = number % 10; //picks off the last value in the integer
number /= 10; //enables the loop to set the last value of the integer to zero
count2 -=1; //loops one less time to get the numbers from front to back
}
//switch statements
switch (lastDigit) {
case 9:
NSLog(@"nine");
break;
case 8:
NSLog(@"eight");
break;
case 7:
NSLog(@"seven");
break;
case 6:
NSLog(@"six");
break;
case 5:
NSLog(@"five");
break;
case 4:
NSLog(@"four");
break;
case 3:
NSLog(@"three");
break;
case 2:
NSLog(@"two");
break;
case 1:
NSLog(@"one");
break;
case 0:
NSLog(@"zero");
break;
default:
break;
}
}
}
return 0;
}
Upvotes: 0
Reputation: 3319
As a learning exercise, I modified Dave's code:
+(NSString*)doIt:(NSString*)inString delimiter:(NSString*)delimiter{
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterSpellOutStyle];
NSMutableString* outString= [[NSMutableString alloc]init];
for (int i=0; i< [inString length]; i++) {
unsigned char oneChar= [inString characterAtIndex:i];
if (oneChar>47 && oneChar<58) {
NSString* temp=[f stringFromNumber:[NSNumber numberWithUnsignedChar:oneChar-48]];
[outString appendFormat:@"%@",temp];
[outString appendString:delimiter];
}
}
[f release];
[outString autorelease];
return outString;
}
Upvotes: 1
Reputation: 4594
Since you're adding the numbers to a string, and you want to calculate them right to left, prepend the string with each new number. Something like:
numberString = [NSString stringWithFormat:@"%@ %@", theNewNumber, numberString];
Where theNewNumber is a string (like @"six") and numberString is the string that you want to output once you're done...
(oh, and don't forget to initialize numberString before you start looping...something like:
NSString *numberString = @"";
=====
Based on the code you just posted, you could either do it mathematically, or just pre-pend a string like this:
Put this variable in your .h file:
NSString *numberString;
Then put this in your .m:
- (void) prependNumber:(NSString *)num {
numberString = [NSString stringWithFormat:@"%@ %@", num, numberString];
}
NSLog(@"Type in your integer.");
scanf("%i", &number);
numberString = @"";
do
{
digit = number % 10;
if (digit == 0)
[self prependNumber:@"zero"];
if (digit == 1)
[self prependNumber:@"one"];
if (digit == 2)
[self prependNumber:@"two"];
if (digit == 3)
[self prependNumber:@"three"];
if (digit == 4)
[self prependNumber:@"four"];
if (digit == 5)
[self prependNumber:@"five"];
if (digit == 6)
[self prependNumber:@"six"];
if (digit == 7)
[self prependNumber:@"seven"];
if (digit == 8)
[self prependNumber:@"eight"];
if (digit == 9)
[self prependNumber:@"nine"];
number /= 10;
}
while (number != 0);
NSLog (@"%@", numberString);
Upvotes: 5
Reputation: 243146
This isn't exactly what you want, but for your consideration:
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterSpellOutStyle];
NSString *s = [f stringFromNumber:[NSNumber numberWithInt:932]];
NSLog(@"%@", s);
[f release];
This will log:
nine hundred and thirty-two
Again, it's not the "nine three two" you want, but it's also nice and short. :)
Upvotes: 12
Reputation: 32258
Well it sounds like you're halfway there if you were able to get the numbers to convert to words already, even if output backwards.
Assuming you're looping through your data, incrementing the index, just start at the character length of the number, decrementing your index backwards, reversing your output.
We can't help you much more without seeing your actual code. ;)
Upvotes: 2
Reputation: 48398
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int a, b, number, logNum, nThNum;
NSLog(@"Please enter a valid integer: ");
scanf("%d", &number); // read input as a decimal integer
if (!number) // if zero or something other than a number is entered output zero
NSLog(@"Zero");
else if (number < 0) { // convert negatives to something that can be used
number = -number;
NSLog(@"(negative)"); // but output negative first then continue as usual
}
logNum = (log10(number) + 1); // find how many digits there are in the number
for (int j=0; j < logNum; j++) {// loop based on number of digits
a = pow(10,logNum-j);
b = pow(10,logNum-1-j);
nThNum = (number % a) / b;// find the nth digit in a number, in our case 1st
switch (nThNum) {// output current digit that was found
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;
}
}
[pool drain];
return 0;
}
Well, now that you've posted your code, your method will work great if you first reverse the number. So, you can just write a short routine to do that, then use your own code.
Upvotes: 2