• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

How to write this algorithem into C?

mOeeOm

Platinum Member
What I need to do, is take any letter and convert it into another letter based on a key using the caesar cipher.

The code for encryption is: (x + key)mod26

So say for the letter A, A = 0 and my key is 56, so it would be (56)mod26 which is 4, so A = 0, B = 1, C = 2, D = 4, so A becomes D.

How do I write this algorithem? I have the chars as:

char PlainAlpha[]={"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
char CaesarAlpha[]={"XYZABCDEFGHIJKLMNOPQRSTUVW"};

Where plain is what is entered and caesar is the encryption.

thanks for any help.
 
int key = 56;
char x = 'A'; // or whatever character.
x -= 64; // woot, now it's a number based on the charcter's ASCII value.
x += key;
x %= 26;
x += 64;
printf("Letter: %c\n", x);

Now let me test and see if that actually works.

Edit: "65" changed to "64", and it works.
 
Originally posted by: notfred
int key = 56;
char x = 'A'; // or whatever character.
x -= 64; // woot, now it's a number based on the charcter's ASCII value.
x += key;
x %= 26;
x += 64;
printf("Letter: %c\n", x);

Now let me test and see if that actually works.

Edit: "65" changed to "64", and it works.

Thanks, now I just gotta put it in my code right 🙂
 
Originally posted by: notfred
int key = 56;
char x = 'A'; // or whatever character.
x -= 64; // woot, now it's a number based on the charcter's ASCII value.
x += key;
x %= 26;
x += 64;
printf("Letter: %c\n", x);

Now let me test and see if that actually works.

Edit: "65" changed to "64", and it works.

Hmm if its for whatever character, shouldn't be CharPlainAlpha[]?
 
Originally posted by: mOeeOm
Originally posted by: notfred
int key = 56;
char x = 'A'; // or whatever character.
x -= 64; // woot, now it's a number based on the charcter's ASCII value.
x += key;
x %= 26;
x += 64;
printf("Letter: %c\n", x);

Now let me test and see if that actually works.

Edit: "65" changed to "64", and it works.

Hmm if its for whatever character, shouldn't be CharPlainAlpha[]?

It takes any character A to Z and works on that. I don't care where you store your characters beforehand or what you name the array of them.
 
Originally posted by: notfred
Originally posted by: mOeeOm
Originally posted by: notfred
int key = 56;
char x = 'A'; // or whatever character.
x -= 64; // woot, now it's a number based on the charcter's ASCII value.
x += key;
x %= 26;
x += 64;
printf("Letter: %c\n", x);

Now let me test and see if that actually works.

Edit: "65" changed to "64", and it works.

Hmm if its for whatever character, shouldn't be CharPlainAlpha[]?

It takes any character A to Z and works on that. I don't care where you store your characters beforehand or what you name the array of them.

kk heres the code, but when I run it, I had my plaintext.txt file have ABCDEFG and when I run the program, I only get the first letter encrypted :*(
 
notfred's code was for a single character. You need to do it for every character in your array. Think for loop.
 
Originally posted by: UCJefe
notfred's code was for a single character. You need to do it for every character in your array. Think for loop.

I tried fixing it like that...no go what did I do wrong?

 
use the code that i wrote.. it'll take into factor characters that aren't alphabetic and both upper and lower case characters.

you won't need to create any arrays with either mine or notfred's algorithm, which are essentially the same.
 
Originally posted by: mOeeOm
Nope 😛 I'm really bad at programming and this i my first time taking it, its only one more lab after this then I never ever have to see it again

You don't have an exam for it?
 
Originally posted by: Atheus
Originally posted by: mOeeOm
Nope 😛 I'm really bad at programming and this i my first time taking it, its only one more lab after this then I never ever have to see it again

You don't have an exam for it?

Ya, but the exams are mostly the theory and tracing the codes, not writing them. I do have a practical though on March 3rd...t.t
 
Originally posted by: itachi
use the code that i wrote.. it'll take into factor characters that aren't alphabetic and both upper and lower case characters.

you won't need to create any arrays with either mine or notfred's algorithm, which are essentially the same.

I ran your code, doesn't seem to make a ciphertext.txt file, I tried editing it to make it spit out the file, but it wont.
 
You've all but had this done for you by two different people, can't you put a little effort into finishig the thing up yourself?
 
Errr nvm I got it to spit out the files, but another problem came up...

I run the program, in the plaintext i put ABCD but the ciphertext comes out as AA..

Why does this happen?
 
whoops..

it should be
fputc(tmp, ofp)... not fputc(ch, ofp)..

if you set the input text as "Hello World!" the output will be "Ebiil Tloia!"

your problem was that you were writing the character to the file twice and closing the file handles too soon.

my problem, i think, was that i never closed the file handles.. so the buffered text never got flushed out to file.
 
Originally posted by: itachi
whoops..

it should be
fputc(tmp, ofp)... not fputc(ch, ofp)..

if you set the input text as "Hello World!" the output will be "Ebiil Tloia!"

your problem was that you were writing the character to the file twice and closing the file handles too soon.

my problem, i think, was that i never closed the file handles.. so the buffered text never got flushed out to file.

Thanks a lot man, you saved me, I SHOULD be able to finish it from here on by my own...but you never know 🙁, wish I took programming in high school and made my life easier.
 
Back
Top