Need help with a basic C cryptography program

Status
Not open for further replies.

Biggiesized

Ars Tribunus Militum
1,755
So, as part of my punishment homework for an online C class, I have to write a program that solves this assignment:

"Your application should read the four-digit integer entered by the user and encrypt it 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 the first digit with the third, and swap the second digit with the fourth. Then print the encrypted integer."

I had to re-read it slowly because the sentence following the colon is unintuitive. My professor gave me an example. He inputted the sequence "1234" which resulted in the encoded output, "0189".

The reason I'm struggling with this assignment is because I feel I lack the proper knowledge of arguments/statements/etc. at this point to code something like this. After all, we're only in chapter 3 and we've just learned control structures (specificially if, if...else, and while statements) and assignment/increment/decrement operators.

As I understand the problem, it can be broken down in pseudocode as follows:

Read a four digit integer;
Add 7 to each of the digits individually;
If( digit + 7 < 10 --> digit ) else( remainder of sum / 10);
Swap respective digits;
Print output;

If 1234 is used as an example:

1+7 = 8; store 8;
2+7 = 9; store 9;
3+7 = 10; store remainder = 0;
4+7 = 11; store remainder = 1;

Result is 8901 after first sequence of operations. Now swap the digits to get proper encoded output, 0189.

The major problem I have is that I have no idea how to store individual digits as their own variables. :confused: Nothing in the chapter has even alluded to this. The only libraries we've included thus far in our programming assignments are <stdio.h> and <stdlib.h> (the latter for pausing the DOS window).

I'd appreciate if anyone could lend me any help.
 

arikalish

Ars Centurion
245
Subscriptor
How were you planning on getting the input from the command line? You might look at scanf:

http://www.cplusplus.com/reference/clib ... dio/scanf/

Something like:

Code:
int d1, d2, d3, d4;
int newD1, newD2, newD3, newD4;
int count = scanf("%1d%1d%1d%1d", &d1, &d2, &d3, &d4);

// check to make sure we got enough digits
if (count != 4)
{
    return -1;
}

That will get you the four digits as integers. You can then manipulate them individually and print them back out:

Code:
//do stuff with digits here and put results in newD1 through newD4

printf("%d%d%d%d", newD1, newD2, newD3, newD4);

Note that d1, newD1, etc. can be replaced by arrays. You'll need to change the calls to scanf and printf a bit.

You can also use getchar(), remembering that when working with characters, subtract '0' to get the integer value.
http://www.cplusplus.com/reference/clib ... o/getchar/

For example:

Code:
int i;
char charDigits[4];
int digits[4];

for (i = 0; i < 4; i++)
{
   // set value to something out of range
   charDigits[i] = 'A';
   // make sure we actually get a digit
   while (charDigits[i] < '0' || charDigits[i] > '9')
   {
          digits[i] = getchar();
   }
   
   digits[i] = charDigits[i] - '0'; // once we know we've got a digit, convert it to an int.

}

I'm leaving out a lot of error checking here, but hopefully it points you in the right direction.
 

Biggiesized

Ars Tribunus Militum
1,755
We haven't covered arrays or getchar(), but we do use scanf().

I figured out a crude method in the meantime:

Code:
    int choice; /* 1 for encode; 2 for decode */
    int input; /* integer to be encoded or decoded */
    int output; /* integer as a result of encoding or decoding */
    int a, b, c, d; /* individual digits */
    
    printf( "Encode (1) Decode (2):\n" );
    
    scanf( "%d", &choice ); /* read input for program decision */
    
    
    if( choice == 1 ) {
        printf( "Enter a four digit number:\n" );
        scanf( "%d", &input ); /* read integer for encoding */
                
        a = input / 1000;
        b = ( input / 100 ) % 10;
        c = ( input / 10 ) % 10;
        d = input % 10;
        
        int a1, b1, c1, d1; /* modulated integers */
        
        if( a + 7 < 10 ) {
            a1 = a + 7;
        }
        else
        {
            a1 = ( a + 7 ) % 10;
        }
        
        if( b + 7 < 10 ) {
            b1 = b + 7;
        }
        else
        {
            b1 = ( b + 7 ) % 10;
        }
        
        if( c + 7 < 10 ) {
            c1 = c + 7;
        }
        else
        {
            c1 = ( c + 7 ) % 10;
        }
        
        if( d + 7 < 10 ) {
            d1 = d + 7;
        }
        else
        {
            d1 = ( d + 7 ) % 10;
        }
                        
        printf( "Encoded digits: %d%d%d%d\n", c1, d1, a1, b1 );

I do have to write a decoding algorithm, but at least the first part is settled.

My final program session should look like this, according to the instructor:

Code:
Encode (1) Decode (2): 1
Enter a four digit number: 1234
Encoded Digits: 0189
Continue (1) Exit (0): 1
Encode (1) Decode (2): 2
Enter a four digit number: 0189
Decoded Digits: 1234
Continue (1) Exit (0): 0

Any idea how to write the exit code?

Code:
if( choice == 0 ) {
...
:confused:
 

arikalish

Ars Centurion
245
Subscriptor
Some notes:

See my use of scanf and separate variables, it'll save all the /10, /100 trouble.

This code:

Code:
        if( a + 7 < 10 ) {
            a1 = a + 7;
        }
        else
        {
            a1 = ( a + 7 ) % 10;
        }

is equivalent to this:

Code:
 a1 = (a + 7)  % 10

If a = 2, then a+7 = 9. 9 % 10 = 9. Might as well keep the code cleaner.

As for the exit, how about wrapping the whole program in a loop and doing something like this:

Code:
// set to something that will fail the loop condition below
char returnCode = '2';
int count;
while (returnCode != '0')
{
   // do your encrypting/decrypting

    while (returnCode != '0' && returnCode != '1')
    {
       printf("Continue (1) Exit (0): \n");
       count = scanf("%c", &returnCode)
       \\ do something with count?
    }
}
 

Biggiesized

Ars Tribunus Militum
1,755
arikalish":1wj4xi5c said:
Some notes:

See my use of scanf and separate variables, it'll save all the /10, /100 trouble.

This code:

Code:
        if( a + 7 < 10 ) {
            a1 = a + 7;
        }
        else
        {
            a1 = ( a + 7 ) % 10;
        }

is equivalent to this:

Code:
 a1 = (a + 7)  % 10

If a = 2, then a+7 = 9. 9 % 10 = 9. Might as well keep the code cleaner.

Thanks a lot. I was afraid % would return 1 or 0 since all of the examples in the book only show the operand in use when the numerator is greater than the denominator.
 

Biggiesized

Ars Tribunus Militum
1,755
DeathRayLoveMachine":1zxr0rbr said:
why everyone makes it complicate

your input number is (a), (b), (c), (d) where each of a-d is a number 0-9.

your output number is ((c+7) % 10), ((d+7) % 10), ((a+7) % 10), ((b+7) % 10).

It's a slightly tricky algebra word problem disguised as a programming problem.
I understood that from the get-go, but not how to scan the integers into variables.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
Which is why you write quick test programs to determine whether you hypothesis is correct...

Be warned the modulo operator is one of the most varied in terms of edge cases across programming languages including implementations which are *implementation defined*

It is one of those cases where "It worked on my machine" is not an acceptable response. If you're a proper programmer you learn the rules.
 
Status
Not open for further replies.