Thread: Encrypt a number

1. 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. 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. Originally Posted by aGerman
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. 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. 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. 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. Originally Posted by aGerman
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. > 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.

9. 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. Originally Posted by Salem
> 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. Thanks aGerman, it is useful.

Popular pages Recent additions