Encryption

Hi another exercise which im having problems with. In this challenge im given a four digit integer and encrypt each digit as follows. Replace each digit by the sum of that digit plus seven mod 10. Then swap 1st and 3rd digit places and 2nd with 4th. Heres what i got so far. I havent figured out yet how to swap them. Any thoughts on that ? No arrays allowed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//Deitel problem 2.38
//Encrypted integer

#include "stdafx.h"
#include <iostream>


using namespace std;

int main(){

	int iCounter, iNumber = 4596,
		iRemainder = 0, iCopy, iRemOfOriginal = 0;
	

		iCopy = iNumber;

		for ( iCounter = 0; iCounter <= 1; iCounter++) {

			do {
			
				iRemainder *= 10;

				if ( iCounter == 0) 				
					iRemainder += ((iNumber % 10) + 7) % 10;		
				else
					iRemainder += iNumber % 10;

				iNumber /= 10;				
			
			}while( iNumber != 0 );

			iNumber = iRemainder;
			iRemainder = 0;
		}

		cout << " Old number " << iCopy << " new number " << iNumber << endl;

	cin.get();
	cin.get();
	return 0;
}
1234
3412
So it's a rotation

You could get advantage of the fixed numbers of digits
1
2
3
4
5
6
//back element
d=n%10;
//pop_back
n/=10;
//push_front()
n+= d*1000;

Last edited on
Yeah i tried this method earlier
1
2
3
4
number = 1235;
2	rem = number % 100;
3	whole = number / 100;
4	whole += rem * 100

but it dont always work. ill test youre method
I dont know if anyone interested in this , but this is what i came up with. Not very elegant but works, so please do critique.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//Deitel problem 2.38
//Encrypted integer

#include <iostream>


using namespace std;

int main(){

	int iCounter, iNumber = 4596, iDigit,
		iRemainder = 0, iCopy, iRemOfOriginal = 0;
	

		iCopy = iNumber;

		for ( iCounter = 0; iCounter <= 1; iCounter++) {

			do {
			
				iRemainder *= 10;

				if ( iCounter == 0) 				
					iRemainder += ((iNumber % 10) + 7) % 10;		
				else
					iRemainder += iNumber % 10;

				iNumber /= 10;				
			
			}while( iNumber != 0 );

			iNumber = iRemainder;
			iRemainder = 0;
		}

		//This is where i swap digit places.
		//Find third digit
		
		iDigit = (iNumber % 100) / 10;
	
		do{

			iRemainder = iNumber % 10;
			iRemainder *= 1000;
			iRemainder = iRemainder + iNumber / 10;
			iNumber = iRemainder;

		}while ((iNumber / 1000)  != iDigit );


		cout << " Old number " << iCopy << " new number " << iNumber << endl;

	return 0;
}
Personally, rather than go through a lot of if/else do/while statements, I'd just split
the original number into its four constituent digits (call them ones, tens, hundreds,
thousands), perform the + 7 % 10 on each digit, then reform the new number.
It means duplicating the + 7 % 10 line of code four times, but all in all, your
program will be all of about 12 lines of code then instead of 50.
I dont know why i over complicate things. Thanks its just that this chapter is dealing with loops and if else statements and im automatically think that authors intentions was to use loops.
Topic archived. No new replies allowed.