So I am making a Caesar cipher program I have the encryption working just fine it is the decryption I am having trouble with.
Here is the code that works for encryption on uppercase letters:
if (isupper(p[i])){
char c = ((p[i] - 'A') + k % 26) + 'A';
}
Now I would think that decryption would be:
if (isupper(pp[i])){
char c = (abs((p[i] - 'A') - k) % 26) + 'A';
}
The issue that I'm having is that if k=1 and p[i]='A' it will just output the letter 'B' and not 'Z' it does not wrap around like it should so if k=2 and p[i]='A' it should output the letter 'Y'. It works if k=1 and p[i]='B' it will output 'A' thanks for any help.
This should do it:
if (isupper(p[i])){
char c = (p[i] - 'A' + 26 - k) % 26) + 'A';
}
The + 26 gives you your wrapping
Think of it this way (the three digit number format is just to make things line up nicely):
| 000 | 001 | 002 | ... | 024 | 025 | plain
| A | B | C | ... | Y | Z |
| 003 | 004 | 005 | ... | 001 | 002 | crypto
Now, recognize that
(n + 26) % 26 === (n % 26)
So:
| 000 | 001 | 002 | ... | 024 | 025 | plain
| A | B | C | ... | Y | Z |
| 029 | 030 | 031 | ... | 053 | 054 | crypto
is equivalent to the above when you consider modulo 26
This makes life a lot easier. The plaintext symbol set is made up of a contiguous set of integers from 'A' to 'A' + 25. The problem is that the ciphertext symbol set is not contiguous... there's a discontinuity at 025. By adding 26, you can convert the cipher text to a contiguous range from 'A' + k + 26 to 'A' + k + 49.
It's much easier then to map your contiguous ciphertext symbol set to your plaintext symbol set.
Since the Caesar code could be a shift in either direction and the decrypt is just the opposite shift, you could combine this to
boolean decrypt;
int k;
...
k = k % 26; // Ensure that the shift doesn't include any wrapping
if(decrypt) {
k *= -1;
}
if (isupper(p[i])){
char c = (p[i] - 'A' + 26 + k) % 26) + 'A';
}
In the encryption,
((p[i] - 'A') + k % 26) + 'A'
consider that it is evaluated as
t1 = p[i] - 'A'
t2 = k % 26
result = t1 + t2 + 'A'
That probably is not what you intended. The decryption would not reverse that arithmetic, mostly because the extra grouping changes the order of operations.
You are not considering the fact that they wrap around, try it like this:
if ((p[i] - 'A' - k) < 0 )
c = 'Z' - (abs(p[i] - 'A' - k) % 26) + 1;
else
c = (abs(p[i] - 'A' - k) % 26) + 'A';
And I think you don't need the moudulo "%" either.
Related
I am currently trying to convert an equation into simpler form and found out that my codes are over writing the value at the end of the loop.
I have found out similar discussions but not exactly what I am looking for. Most of articles were using other languages so I couldn't get the answer.
Any answers are appreciated and thanks in advance.
followings are my code
int index = 0;
int result = 0;
char tresult[100];
char *equation[100] = { NULL, };
char *temp = strtok(input, " ");
for(int i = 1; i < x; i = i + 2)
{
char *temp_sign = equation[i];
if(*temp_sign == '*')
{
result = atoi(equation[i - 1]) * atoi(equation[i +1]);
sprintf(tresult, "%d", result);
equation[i - 1] = tresult;
sprintf(equation[i], "%d", 0);
sprintf(equation[i + 1], "%d", 0);
}
}
for(int j = 0; j < x; j++)
{
printf("%s ", equation[j]);
}
Expected input
5 * 3 + 1 * 2
targeted output
15 0 0 + 2 0 0
I will remove 0 by adding extra codes to make it as
15 + 2
but currently, my output looks like
2 0 0 + 2 0 0
When I print out the value in the loop, all the values were correctly shown. What may be the cause of such problem?
It might be easier to understand if we draw out the pointer instead.
Assuming that input is initially equal to "5 * 3 + 1 * 2", then after the loop equation will look something like this:
+-------------+
| equation[0] | ------------------------\
+-------------+ |
| equation[1] | --> | input[2] | ... | | +------------+-----+
+-------------+ >--> | tresult[0] | ... |
| equation[2] | --> | input[4] | ... | | +------------+-----+
+-------------+ |
| equation[3] | ------------------------/
+-------------+
| equation[4] | --> | input[10] | ... |
+-------------+
| equation[5] | --> | input[12] | ... |
+-------------+
As seen in the above "drawing" both equation[0] and equation[3] will be pointing to the first character of the single array tresult.
And tresult will always contain the contents last written into it with sprintf(tresult, "%d", result). Which in your example will be "2".
res = 1;
for ( i = 1; i <= n; i <<= 1 ) // n = exponent
{
if ( n & i )
res *= a; // a = base
a *= a;
}
This should be more effective code for power and I don't know why this works.
First line of for() is fine I know why is there i <<= i. But I don't understand the line where is: if ( n & i ). I know how that works but I don't know why...
Let us say you have a binary representation of an unsigned number. How do you find the decimal representation?
Let us take a simple four bit example:
N = | 0 | 1 | 0 | 1 |
-----------------------------------------
| 2^3 = 8 | 2^2 = 4 | 2^1 = 2 | 2^0 = 1 |
-----------------------------------------
| 0 | 4 | 0 | 1 | N = 4 + 1 = 5
Now what would happen if the base wasn't fixed at 2 for each bit but instead was the square of the previous bit and you multiply the contribution from each bit instead of adding:
N = | 0 | 1 | 0 | 1 |
----------------------------
| a^8 | a^4 | a^2 | a^1 |
----------------------------
| 0 | a^4 | 0 | a^1 | N = a^4 * a^1 = a^(4+1) = a^5
As you can see, the code calculate a^N
I got the following Karnaugh Maps but I am still having problems working out the expression for XOR from each table.
Table 1
-------
WZ
00 01 11 10
-----------------------
00 | | | | 1 |
-----------------------
01 | 1 | | | |
-----------------------
XY 11 | | | | 1 |
-----------------------
10 | 1 | | | |
-----------------------
Table 2
-------
WZ
00 01 11 10
-----------------------
00 | | 1 | | |
-----------------------
01 | | | 1 | |
-----------------------
XY 11 | | 1 | | |
-----------------------
10 | | | 1 | |
-----------------------
It is XORs, but how can I easily deduce the XOR expressions?
I would not dismiss the variable z from the expression, because I think, the expression ¬z·(¬x·y·¬w + ¬x·w·¬y + ¬y·¬w·x + w·y·x) is not equal to (¬x·y·¬w + ¬x·w·¬y + ¬y·¬w·x + w·y·x). That would mean, that the K-map contains four doubles of ones, but there is only four singles.
I would rather find the expression in the K-map and then use the laws of Boolean algebra.
For the first table:
¬x·¬y·w·¬z + ¬x·y·¬w·¬z + x·y·w·¬z + x·¬y·¬w·¬z
¬z·((¬x + ¬y + w)·(¬x + y + ¬w)·(x + y + w)·(x + ¬y + ¬w)) //distributivity
¬z· (¬x + ¬y + w)·(¬x + y + ¬w)·(x + y + w)·(x + ¬y + ¬w) //relaxed syntax
¬z· (¬x·¬x + ¬x·y + ¬x·¬w + ¬y·¬x + ¬y·y + ¬y·¬w + w·¬x + w·y + w·¬w)·
(x·x + x·¬y + x·¬w + y·x + y·¬y + y·¬w + w·x + w·¬y + w·¬w) //distributivity
Because of the laws of
idempotence (e.g.: ¬x·¬x=¬x),
absorption (e.g.:¬x + ¬x·y=¬x)
and complementation (e.g.: ¬x·x=0)
the expression is equivalent to:
¬z· (¬x + 0 + ¬y·¬w + w·y + 0)·
( x + + 0 + y·¬w + + w·¬y + 0 )
¬z· (¬x + ¬y·¬w + w·y)·(x + y·¬w + w·¬y) //just formatted
¬z· (¬x·x + ¬x·y·¬w + ¬x·w·¬y
+ ¬y·¬w·x + ¬y·¬w·y·¬w + ¬y·¬w·w·¬y
+ w·y·x + w·y·y·¬w + w·y·w·¬y) //distributivity
¬z· ( 0 + ¬x·y·¬w + ¬x·w·¬y
+ ¬y·¬w·x + 0 + 0
+ w·y·x + 0 + 0 ) //using the three laws↑ again
¬z· (¬x·y·¬w + ¬x·w·¬y + ¬y·¬w·x + w·y·x) //how the 3-input XOR is defined
¬z· (x xor y xor w)
For the second table:
¬x·¬y·¬w·z + ¬x·y·w·z + x·y·¬w·z + x·¬y·w·z
z·((¬x + ¬y + ¬w)·(¬x + y + w)·(x + y + ¬w)·(x + ¬y + w)) //distributivity
z· (¬x + ¬y + ¬w)·(¬x + y + w)·(x + y + ¬w)·(x + ¬y + w) //relaxed syntax
z· (¬x·¬x + ¬x·y + ¬x·w + ¬y·¬x + ¬y·y + ¬y·w + ¬w·¬x + ¬w·y + ¬w·w)·
(x·x + x·¬y + x·w + y·x + y·¬y + y·w + ¬w·x + ¬w·¬y + ¬w·w) //distributivity
z· ( ¬x + + 0 + ¬y·w + + ¬w·y + 0 )·
( x + + 0 + y·w + + ¬w·¬y + 0 )
z· (¬x + ¬y·w + ¬w·y)·(x + y·w + ¬w·¬y) //just formatted
z· (¬x·x + ¬x·y·w + ¬x·¬w·¬y
+ ¬y·w·x + ¬y·w·y·w + ¬y·w·¬w·¬y
+ ¬w·y·x + ¬w·y·y·w + ¬w·y·¬w·¬y) //distributivity
z· ( 0 + ¬x·y·w + ¬x·¬w·¬y
+ ¬y·w·x + 0 + 0
+ ¬w·y·x + 0 + 0) //using the three laws↑ again
z· (¬x·y·w + ¬x·¬w·¬y + ¬y·w·x + ¬w·y·x) //how the 3-input XNOR is defined
z· (x xnor y xnor w)
The first table contains an Xor expression :
`First table`
w
\ wz ___________
xy \-----------------------+
| | | | 1 |
+-----+-----+-----+-----+
| 1 | | | | |
+-----+-----+-----+-----+ | y
| | | | | 1 | |
x | +-----+-----+-----+-----+
| | 1 | | | |
+-----------------------+
___________
z
as you could see the middle of the table (Z area) is fake.
that is, the table function is :
F(Table1) = w'x'yz' + wx'y'z' + w'xy'z' + wxyz'
in binary form you could see a zero column :
F(Table) = 0010 eliminating Z F(xor)= 001
0100 ---------------\ 010
1110 ---------------/ 111
1000 100
^--> fake
and the final table must be something like this :
`simplified xor table`
w
\ w 0 __1__
xy \-----------+
00 | | 1 |
+-----+-----+
01 | 1 | | |
+-----+-----+ | y And " F = wy' + w'y " is an Xor only
|10 | 1 | | | between 2 variables, right?
x | +-----+-----+
|11 | | 1 |
+-----------+
The second table just contains an Xnor expression of the first one :
`Second Table`
F(Table2) = w'xyz + wxy'z + w'x'y'z + wx'yz
w
\ wz ___________
xy \-----------------------+ negation of table 2 is table 1 and vise versa
| | 1 | | | F(Table2) = 1101 F(Table2)'= F(Table1) = 0010
+-----+-----+-----+-----+ 1011 0100
| | | 1 | | | 0001 1110
+-----+-----+-----+-----+ | y 0111 1000
| | | 1 | | | | ^--> fake ^
x | +-----+-----+-----+-----+
| | | | 1 | |
+-----------------------+
^ ___________ ^
^ z ^
^ ^
^--------z'-------^
the final table is:
w
\ w 0 __1__
xy \-----------+
00 | 1 | |
+-----+-----+
01 | | 1 | |
+-----+-----+ | y And " F = w'y' + wy " is an Xnor
|10 | | 1 | |
x | +-----+-----+
|11 | 1 | |
+-----------+
Always remember the tables that contain the zigzag pattern
are either an Xor or Xnor expression.
Just put a copy of this map on the right hand side of it (or left, no difference) and then choose two tilted cubes.
Now, we write the simplified function for both of them:
(A = 1) (AND) (B=0 when C=1 and B=1 when C=0)
(OR)
(A = 0) (AND) (B=0 when C=0 and B=1 when C=1)
that finaly gives this:
(A AND (B XOR C)) OR (¬A AND (B XNOR C))
Basic rule for xor is that it gives 1 when odd number of input are 1.
So in KMAP just see if 1 is present in all the odd number of 1's.
Like WXYZ ( 0010, 1110 etc) if all gives 1 than there is a XOR in kmap.
I have to printfs in a loop an I want to print the output in two lines, instead of intermingled on one line.
Like this:
printf("| %-7.2f ", Fahrenheit);
which produces:
| -508.00 | -463.00 | -418.00 | -373.00 | -328.00 | -283.00 |
When I add printf("| %-6d", Celsius); under the printf above, it prints right next to/in the middle of my first printf.
I want the output to be like:
| -508.00 | -463.00 | -418.00 | -373.00 | -328.00 | -283.00 |
| -300 | -275 | -250 | -225 | -200 | -175 | -150 | -125|
instead of the two sets of values being intermingled on the same line.
part of my code:
for(Celsius = CelsiusMin;Celsius <= CelsiusMax; Celsius += CelsiusStep)
{
Fahrenheit = FahrenheitZero + Celsius * CelsiusToFahrenheit;
printf("| %-7.2f ", Fahrenheit);
printf("| %-6d", Celsius);
}
return EXIT_SUCCESS;
}
You can't directly have one sequence of printf() statements writing to line 1 and a second concurrent sequence of printf() statements writing to line 2, so you're going to have to fake it.
You probably need something that builds up the two lines of output, and then prints them when they're ready:
char line1[128];
char line2[128];
char *next1 = line1;
char *next2 = line2;
for (int c = -325; c <= -125; c += 25)
{
double f = (c + 40.0) * (9.0 / 5.0) - 40.0;
next1 += sprintf(next1, "| %-7.2f ", f);
next2 += sprintf(next2, "| %-7d ", c);
}
printf("%s|\n", line1);
printf("%s|\n", line2);
Sample output:
| -553.00 | -508.00 | -463.00 | -418.00 | -373.00 | -328.00 | -283.00 | -238.00 | -193.00 |
| -325 | -300 | -275 | -250 | -225 | -200 | -175 | -150 | -125 |
The conversion formula is simpler than the usual one you see quoted, and is symmetric for converting °F to °C or vice versa, the difference being the conversion factor (9.0 / 5.0) vs (5.0 / 9.0). It relies on -40°C = -40°F. Try it:
C = 0°C; (C+40) = 40; (C+40)*9 = 360; (C+40)*9/5 = 72; (C+40)*9/5-40 = 32°F.
F = 32°F; (F+40) = 72; (F+40)*5 = 360; (F+40)*5/9 = 40; (F+40)*5/9-40 = 0°C.
You might do better with the degrees Celsius as printed as a double too; I kept the integer representation you chose, but made sure the numbers line up on the two lines.
FYI: Absolute zero is -273.15°C, 0K, -459.57°F. So you're attempting to print non-existent temperatures.
Here is a program to swap two numbers with out using temporary variable and using shifting operations:
#include <stdio.h>
#include <conio.h>
int main(void)
{
int a,b,i,j;
clrscr();
printf(“Enter two integers: “);
scanf(“%d%d”,&a,&b);
printf(“a=%d,b=%d\n”,a,b);
for(i = 0; i < 16; i++)
{
if((a & (1 << i)) ^ (b & (1 << i)))
{
a = a ^ (1 << i);
b = b ^ (1 << i);
}
}
printf(“a=%d,b=%d”,a,b);
getch();
return 0;
}
My question is what is significance of 1 in this program?
I know the method of xoring that works as follows
a = a^b;
b = a^b;
a = a^b;
but I don't know how above program works?
It toggles each bit if only one is set.
c = a & (1 << i) = true if the ith bit of a is set
d = b & (1 << i) = true if the ith bit of b is set
| c | d | Action | c' | d' |
-------------------------------------
| 0 | 0 | Do nothing | 0 | 0 |
| 0 | 1 | Toggle the bits | 1 | 0 |
| 1 | 0 | Toggle the bits | 0 | 1 |
| 1 | 1 | Do nothing | 1 | 1 |
1 has one bit on the rightmost position set. 1<<i has one bit on place i set. This program loops through each bit, and swaps them if they are different.
a&(1<<i) tests if a has bit i set.
((a&(1<<i))^(b&(1<<i))) tests if bit i in a and b are different.
a=a^(1<<i) toggles bit i.
It's similar to the XOR trick, but swaps only a single bit at a time and only if this bit actually differs in a and b.
1<<i has bit i set to 1 and all other bits 0.
Also, this does not swap two numbers without using a temporary variable. It uses the temporary i.