简体   繁体   中英

How do I make the numbers in this C program print in the correct order and not in reverse?

I am new to C so I am having a little difficulty!

I want to take an integer input from the user and add 7 to each of the digit in the input. All of that works, but the digits are printing in the reverse order.

How do i make the digits print in the correct order? I checked other similar questions on Stack overflow but it does not seem to work. Thanks in advance!


int main(void)
{
    int numToEncrypt;

    printf("Please input a 4-digit number you wish to encrypt:  ");
    scanf("%d", &numToEncrypt);

    while (numToEncrypt > 0) 
    {
        int digit = numToEncrypt % 10;
        // do something with digit
        digit = (digit + 7)%10;
        numToEncrypt /= 10;
        
        
    printf("number is: %d \n",digit);
    
    
    }
}
)

Converting the string input to an integer and back is pointless. Just work with the data as a string. eg:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main(void)
{
        int c;

        if( getenv("V") ) {
                printf("Please input the number you wish to encrypt:  ");
                fflush(stdout);
        }
        while( (c = getchar()) != EOF ) {
                if( isspace(c) ) {
                        fflush(stdout);
                } else if( isdigit(c) ) {
                        c = '0' + (c - '0' + 7) % 10;
                } else {
                        fprintf(stderr, "Invalid input: %c", c);
                        return EXIT_FAILURE;
                }
                putchar(c);
        }
}

Note that a huge advantage of doing this is that it is easy to work with ten million digit integers. You will not be able to do that using scanf to convert the string into an integer.

One way is using a variable to specify which digit to process.

#include <stdio.h>

int main(void)
{
    int numToEncrypt;
    int delta = 1000; // for 4-digit number

    printf("Please input a 4-digit number you wish to encrypt:  ");
    scanf("%d", &numToEncrypt);

    while (delta > 0) 
    {
        int digit = (numToEncrypt / delta) % 10;
        // do something with digit
        digit = (digit + 7)%10;
        delta /= 10;


        printf("number is: %d \n",digit);


    }
}

As this is homework, you could use recursion:

#include <stdio.h>

void print_recursive(int num)
{
    // print leading digits
    if (num>9)
    { 
        print_recursive(num/10);
    }

    // print last digits
    printf("number is: %d\n", (num+7)%10);
}

int main(void)
{
    int number;

    printf("Please input a 4-digit number you wish to encrypt:  ");
    scanf(" %d", &number); // note: You should check the return value!
    print_recursive(number);
}

It is not limited to 4 digits.

For a simple program like this, one usually does not bother with a lot of design. However, it is also beneficial to practice software design on simple problems like this, since the knowledge extends to more complicated programs. This is an application of divide and conquer (as a problem solving strategy, not the computer algorithm). The idea being that smaller problems are simpler than larger ones.

In this case, you consider encapsulating the work of "encrypting" to a function, and have the function return the encrypted value. We'll just implement a stub for now, and fill it in later.

int encryptBy7(int input) {
    int output = 0;
    return output;
}

In addition, we can encapsulate the work of "printing" to a function. And, this is your actual question, if we think about it critically.

void printDigitByDigit(int num, const char *msg) {
    printf("stub\n");
}

So your main program would look like:

int main(void) {
    int numToEncrypt;
    int numEncrypted;

    printf("Please input a 4-digit number you wish to encrypt:  ");
    scanf("%d", &numToEncrypt);

    numEncrypted = encryptBy7(numToEncrypt);
    printDigitByDigit(numEncrypted, "number is");

    return 0;
}

So, your algorithm to encrypt seems to work, so let's just code that up in a way that it stores it as a number.

int encryptBy7(int input) {
    int output = 0;
    int pow10 = 1;

    /* Original program didn't deal with 0 */
    if (input == 0) return 0;

    while (input > 0) {
        int digit = input % 10;
        // do something with digit
        digit = (digit + 7)%10;
        input /= 10;
        // build output
        output += digit * pow10;
        pow10 *= 10;
    }

    return output;    
}

So, now we get to the meat of your question, which is about how to print out the digits starting with the most significant digit. If we see how we built up the output in the previous function, we can reverse the same process of looking at the powers of 10 to find the most significant digit, and then work backwards from there.

void printDigitByDigit(int input, const char *msg) {
    int pow10 = 1;
    int x = input;

    // Find the position of the most significant digit
    while (x > 10) {
        pow10 *= 10;
        x /= 10;
    }

    // Divide by the input appropriate power of 10 to
    // find and print the corresponding digit
    while (pow10 > 0) {
        int digit = (input / pow10) % 10;
        printf("%s: %d\n", msg, digit);
        pow10 /= 10;
    }
}

Of course, you are free to choose to try to do this as a single program inside of main as you had originally attempted, and the result would probably be a shorter program. I'll leave that as an exercise. However, I would argue that breaking up the program into tasks will provide you more benefit in the long run, and itself is a problem solving tool. Each function becomes easier to think about, and thus an easier problem to solve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM