I've written this code. For now it wonly works with small caps letter, I will add Capital letters once I get it working.
The main idea is that J will start at 0 and follow up one step so it can be used in the calculation for the:
printf("%c", ((((p[J] + k[moduled]) % 26))+85 ));
formula
moduled is a variable that starts at 0 and it wraps around.
Everything goes great until the last letter of the keyword.
I've been at this problem over a week now and I can't seem to know the reason it does not cypher correctly at the last letter and from there on.
Here's the code and the result after compiling:
{
include <stdio.h>
include <cs50.h>
include <string.h>
include <stdlib.h>
include <ctype.h>
int main(int argc, string argv[])
{
if (argv[1] != NULL)
{
string k = argv[1];
int J = 0; // This variable will be the continious one, for K purposes
for (int j = 0, n = strlen(k); j < n; j++)
{
char h = k[j];
if(( h >= ' ' && h <= '@') || (h >= '[' && h <= '`') || (h >= '{' && h <= '~'))
{
printf("Please enter a keyword to start decryption!!\n");
return 1;
}
else
{
}
}
string p = GetString();
int largo = strlen(k);
for (int i = 0, N = strlen(p); i <= N; i++)
{
do
{
int moduled = (J % largo);
if (moduled == 0 && i == 0)// IF J != DE 0
{
if (p[i] >= 97 && p[i] <= 122)
{
printf("%c", ((((p[J] + k[moduled]) % 26))+85 ));
J = J +1 ;
}
else if (p[i] >= 65 && p[i] <= 90)
{
}
else if (p[i] == 32)
{
printf(" ");
}
}
else if ( moduled > 0)//
{
if (p[i] >= 97 && p[i] <= 122)
{
printf("%c", ((((p[J] + k[moduled]) % 26))+85 ));
J = J +1 ;
}
else if (p[i] >= 65 && p[i] <= 90)
{
}
else if (p[i] == 32)
{
printf(" ");
}
}
else
{
if (p[i] >= 97 && p[i] <= 122)
{
printf("%c", ((((p[J] + k[moduled]) % 26))+85 ));
J = J +1 ;
}
else if (p[i] >= 65 && p[i] <= 90)
{
}
else if (p[i] == 32)
{
printf(" ");
}
}
}
while (i > N);
}
}
else
printf("Please enter a KEYWORD to start decryption!!\n");
return 1;
}
}
./vige2 bacon meet me negh an
Even without spaces ./vige2 bacon meetme negh`f
OK I-ve seen to have narrowed the problem: When I started doing some basic math to get to the desired letter using bacon and meet me at the park example,
m = 109 e = 101 e = 101 t = 116 m = 109 +
b = 98 a = 97 c = 99 o = 111 n = 110
207 198 200 227 219
If i % by 26 I get
26 16 18 19 11
I Require the following Ascii values as the problem states
110 101 103 104 122
So the difference is:
85 85 85 85 12
So my formula for conversion is:
printf("%c", ((((p[J] + k[moduled]) % 26))+85 ));
WHY IS THE DIFFERENCE THE SAME ALWAYS EXCEPT FOR THE LAST LETTER, this is where I am getting the error!!
Please help it has been over a week working on this (including this last weekend)