Thread: Encrypt a number

  1. #1
    Registered User
    Join Date
    Nov 2022
    Posts
    8

    Encrypt a number

    Hello everyone. I am self learning C from the Dietel and Dietel book C How to program. I am doing an assignment at the end of the chapter that requires to encrypt a four digit integer as follows. Replace each digit with the result of adding 7 to the digit and getting the remainder after dividing the new value by 10. Then swap first digit with third, and second with fourth. Then display the digit. I think I can solve the first part of the problem. I am not sure how I will switch the digits places. Any suggestions is appreciated. So far the material have only covered while loop, if, else if, and I can only use what's available to me up to this point. I look forward to your suggestions. Thank you.

  2. #2
    Registered User
    Join Date
    Sep 2022
    Posts
    39
    Start with the first task: You have to do something with each digit of a four-digit integer. How do you separate the digits?

  3. #3
    Registered User
    Join Date
    Nov 2022
    Posts
    8
    Quote Originally Posted by aGerman View Post
    Start with the first task: You have to do something with each digit of a four-digit integer. How do you separate the digits?
    Yes, I think I solved the first part. This is what I have so far.

    Code:
    int main(void){
        int number, newNumber,remainder,diviser = 1000;
    
    
        printf("%s","Enter a four digit number: ");
        scanf("%d",&remainder);
    
    
        while(remainder != 0) {
    
    
            number = remainder / diviser;
            remainder %= diviser;
            newNumber += ((number + 7) % 10) * diviser;
            diviser /= 10;
        }
            printf("%s%d","New formed number: ",newNumber);
     	return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2022
    Posts
    39
    First of all keep in mind that variables are not zero-initialized by default. Adding something to the uninitialized newNumber makes it undefined behavior.
    The math is okay. However, the reason why I asked you to separate the digits in a first step is that you have to reorder them later on. You may use an array to store them, or (in case you didn't yet reach out to the array chapter in the book) four additional variables.

  5. #5
    Registered User
    Join Date
    Nov 2022
    Posts
    8
    Yes, newNumber should have been initialized to 0 at the beginning. I haven't covered arrays yet, therefore I can't use it yet. I am close to solving this without using separate variables. I'll post it as soon as I come up with solution.

  6. #6
    Registered User
    Join Date
    Sep 2022
    Posts
    39
    There's more than one way to skin a cat. Of course it is possible to solve it without additional variables. Just use different factors to move your digits to the right position in the loop. You could make the factors depend on the current value of your diviser variable using if-else or switch-case.

  7. #7
    Registered User
    Join Date
    Nov 2022
    Posts
    8
    Quote Originally Posted by aGerman View Post
    There's more than one way to skin a cat. Of course it is possible to solve it without additional variables. Just use different factors to move your digits to the right position in the loop. You could make the factors depend on the current value of your diviser variable using if-else or switch-case.
    This is how I solved it. I am not sure if this is how it was supposed to be done. I am looking more for validation if I was on the right path or critique. Please let me know.

    Code:
    int main(void)
    {
        int number,newNumber = 0,remainder,diviser = 1000;
    
    
        printf("%s","Enter a four digit number: ");
        scanf("%d",&remainder);
    
    
        while(remainder != 0) {
            number = remainder / diviser;
            remainder %= diviser;
            newNumber += ((number + 7) % 10) * diviser;
            diviser /= 10;
        }
        printf("%s%d\n","New formed number: ",newNumber);
        //swap of the digits
        remainder = newNumber % 100;
        number = newNumber / 100;
        remainder = remainder * 100 + number;
        if (remainder < 1000) {
            if(remainder < 100){
                printf("%s%d","Encrypted number is : 00",remainder);
            }else {
                printf("\n%s%d","Encrypted number: 0",remainder);
            }
        }else {
            printf("\n%s%d","Encrypted number: ",remainder);
        }
    
    
     	return 0;
    }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,463
    > This is how I solved it. I am not sure if this is how it was supposed to be done.
    Given the information you have, it's fine.

    The final 10 lines of code can be done with one.
    Code:
    printf("%s%04d\n","Encrypted number: ",remainder);
    But that depends on whether you know about all the toys in the printf format tool box.

    By convention, newlines in format strings go at the end.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Sep 2022
    Posts
    39
    Just to show you what I had in mind:
    Code:
    #include <stdio.h>
    
    int main(void) {
        int number = 0;
        do {
            fputs("Enter a four digit number: ", stdout);
            scanf("%d", &number);
            while (getchar() != '\n'); // discard all characters not convertible into an int
        } while (number < 1000 || number > 9999);
    
    
        // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    
        /*
        // I
        int digits[4] = { 0 };
        for (int divisor = 1000, idx = 0; idx < 4; divisor /= 10, ++idx) {
            digits[idx] = ((number / divisor + 7) % 10);
            number %= divisor;
        }
        const int newNumber = digits[0] * 10 +
                              digits[1] +
                              digits[2] * 1000 +
                              digits[3] * 100;
        */
    
    
        /*
        // II
        int newNumber = 0;
        for (int divisor = 1000, factor = 10; divisor; divisor /= 10) {
            if (divisor == 100) {
                factor = 1;
            } else if (divisor == 10) {
                factor = 1000;
            } else if (divisor == 1) {
                factor = 100;
            } // else, both divisor and factor still have their initial values
    
    
            newNumber += ((number / divisor + 7) % 10) * factor;
            number %= divisor;
        }
        */
    
    
        // III
        static const int factors[] = { 100, 1000, 1, 10 };
        int newNumber = 0;
        for (const int *iter = factors, *const end = iter + 4; iter < end; ++iter) {
            newNumber += *iter * ((number + 7) % 10);
            number /= 10;
        }
    
    
        // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    
        printf("New formed number: %04d\n", newNumber);
        return 0;
    }
    I and II is using your math with the divisor variable. In I you'll find my original proposal to separate the digits first. In II branching is used to choose the right factor. This will add some unnecessary complexity though. III is what I would have come up after all. No additional branching, and no divisor variable with its additional division operations.

  10. #10
    Registered User
    Join Date
    Nov 2022
    Posts
    8
    Quote Originally Posted by Salem View Post
    > This is how I solved it. I am not sure if this is how it was supposed to be done.
    Given the information you have, it's fine.

    The final 10 lines of code can be done with one.
    Code:
    printf("%s%04d\n","Encrypted number: ",remainder);
    But that depends on whether you know about all the toys in the printf format tool box.

    By convention, newlines in format strings go at the end.
    Oh wow, that's neat. Thanks for that. Totally eliminates the rest of the code. No the book did not cover yet that the printf has this formatting ability. I assume it might go into more details when I'll be going over functions. Thanks a lot for that.

  11. #11
    Registered User
    Join Date
    Nov 2022
    Posts
    8
    Thanks aGerman, it is useful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. AES-256 Encrypt for .xml
    By Requnael in forum C++ Programming
    Replies: 3
    Last Post: 02-11-2010, 05:30 PM
  2. A program in C to encrypt?
    By steven2fox in forum C Programming
    Replies: 2
    Last Post: 02-18-2008, 07:11 AM
  3. When to encrypt?
    By Sentral in forum C++ Programming
    Replies: 12
    Last Post: 12-24-2006, 11:53 AM
  4. encrypt
    By hoodiemama in forum C++ Programming
    Replies: 1
    Last Post: 11-03-2002, 12:29 AM
  5. Encrypt
    By vasanth in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2002, 07:14 AM

Tags for this Thread