gloopit gloopit - 3 months ago 9
C Question

C. My digits are printing (in English) in reverse order? How can I fix this?

A little help please! I am working through the exercises in the book, 'Programming in C'.

I have to write a program that takes in an integer and then extracts and displays each digit of the integer in English.

So if I type in 1234, it should print back 'one two three four'.

Due to this exercise being towards the beginning of the book, it has not yet taught arrays, functions, pointers or strings. Which I assume means I'm not allowed to use any of them. So I have to somehow solve it with a pretty limited amount of options.

What I have written works more or less BUT, the numbers are printed back in REVERSE order. I'm really struggling to find an alternative and have been playing around with the code for the past couple of others.

I realize there is another very similar question posted a couple of years ago, but I'm limited even further with what I can do in this problem, not to mention, his/her problem was significantly more complex.

I'd really appreciate it if you could take a look and offer up some suggestions.

#include <cs50.h>
#include <stdio.h>

int main (void)
{
int digit;

//Accept integer
printf("Choose a number.\n");

int num = GetInt();

// adding minus to the beginning if int is negative
if (num < 0)
{
num = -num;
printf("minus ");
}

// isolate each digit from integer and then print in english

do
{
digit = num % 10;

switch(digit)
{
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;
case 10:
printf("Ten ");
break;
default:
break;
}

num /= 10;

} while(num != 0);

printf("\n");
}

Answer

Assuming you cannot use recursion, since you cannot define your own functions, you need an initial loop to find the leftmost digit position.

The following code uses the place variable to hold 10 raised to the 'number of digits minus 1', treating zero as a single digit number. The main loop divides the (remaining part of the) number by place, prints out that digit, then reduces the number modulo place and divides place by 10 for the next iteration until all digits have been printed (when place is zero).

#include <cs50.h>
#include <stdio.h>

int main (void)
{
    int digit;

    //Accept integer
    printf("Choose a number.\n");

    int num = GetInt();

    // adding minus to the beginning if int is negative
    if (num < 0) 
    {
        num = -num;
        printf("minus ");
    }

    // get 10 to the power of 'number of digits minus 1'
    int place;

    for (place = 1; place <= num / 10; place *= 10)
        ;

    // isolate each digit from integer and then print in english

    do
    {
        digit = num / place;

        switch(digit)
        {
        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;
        default:
            break;
        }

        num %= place;
        place /= 10; 

    } while (place != 0);

    printf("\n");
}
Comments