-
Caesar Cipher
I need to make an encrypt/decrypt Caesar algorithm that asks for the user to put in a key. The following is what I have so far, I started off doing a rot-13 and tried to progress but as you can see, ran into a couple of problems. It is not complete yet because I have to ask for character input, but I'm having trouble seeing where I need to add a string array. Can anyone point me in the right direction and maybe give me a hint on what to do? Thanks.
Code:
#include<stdio.h>
int main()
int crypt;
int x,y;
int key;
char input;
{
printf("Do you want your characters encrypted [1] or decrypted [2]? \n");
scanf("%d", crypt);
printf("Enter a key");
scanf("%d", key);
while((x=getchar())!=EOF && crypt=1)
{
printf("Enter data to encrypt.\n");
fflush(stdin);
gets(input);
if(x >='A' && x <='Z')
{
if((y = x + key) <= 'Z')
putchar(y);
else
{
y = x - key;
putchar(y);
}
}
else if(x >='a' && x <='z')
{
if((y= x + key) <= 'z')
putchar(y);
else
{
y = x - key;
putchar(y);
}
}
else
putchar(x);
}
return 0;
}
-
Since there is no such thing as a string array, adding one will be very difficult.
So in your input thing, you can read a character at a time if you want, there's nothing stopping you. If you want to read in a line at a time, then make input an array of char instead of just a single char.
And there is no correct way to use gets.
-
You need to learn about scanf.
Code:
scanf("%d", &digit);
scanf("%c", &char);
scanf("%s", char array);
The ampersand char in C means "the address of". Since array names ARE their address, they don't
need an ampersand before them when in scanf().
That should help. :)
-
So, does that mean my variables are okay? I'm really confused now because I missed the class my teacher went over character arrays and the book is not helping much. I think the "gets" thing was a typo my teacher made, but he said we needed that to scan for a whole line and not just the first word.
-
You would then need to make x travel through each character in the input array. You would also need to encrypt using a caesar cipher correctly. (Hint: y = x - key is not using a caesar cipher correctly.)
-
Would it not be faster to have something like:
Example:
Code:
static unsigned char ascii[256];
int init(int seed)
{
register unsigned char *s = ascii, uc_seed = (unsigned char)seed;
register const unsigned char *end = ascii + sizeof(ascii);
for(;s < end;)
*s++ = uc_seed++;
}
Then you would simply just encode by saying ascii[letter_you_wish_to_encode].
-
Except for "punctuation doesn't change" and "arithmetic mod 26" parts.
-
Oh well, I guess this is what happens when you only deal with me when it is the end of the day and I am tired.
-
Code:
int main()
int crypt;
int x,y;
int key;
char input;
since when the main has such strange number of parameters?
it should be
Code:
int main(void)
{
int crypt;
int x,y;
int key;
char input;
etc...
fflush(stdin); - undefined - read FAQ
crypt=1 - this is assignment you want crypt==1