I'm working in a project and part of it is to do a small encryption program. W can use substitution encryption where each character is substituted by another pre-determined character. Can someone kindly help me with an algorithm. I just can't think any more!

Thanks

drjay

To encrypt:
while string
iteration of char = iteration of char + pre-determined character

To decrypt:
The opposite.

Google, "rot13" That's as easy as it gets... well, actually using bit-operations are easier by a few lines less typed.

Comments
Use rot13 twice to decode this secret message ;)

A simple solution (but will give more flexibility): define two arrays, one with source characters and the other with destination characters to replace those with. Arrays are matched by indices. Just make sure there are no repeats or decryption may not work correctly.

You can also look up RSA or DES if you are dealing with delicate data. But they are both quite complicated to implement (compared to Caesar cipher and rot13).

Can someone explain to me what I'm doing wrong

#include <stdio.h>

main(){
	char ch = 'd';
	char alp[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; 
	char enc[26] = {'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e'};
	for(int i = 0; i <= 26; i++){
		if(ch = alp[i]){
			printf("%c", enc[i]);
		}
	}
return 0;
}

OUTPUT:
fghijklmnopqrstuvwxyzabcde

Prints the whole enc array in order.

How do you print an individual character, not the entire array in C?

>for(int i = 0; i <= 26; i++){
I'd start with this. When you declare an array of 26, it means that the last valid index is 25. This loop runs off the end of the array.

>if(ch = alp){
This is a common mistake too. The operator for equality is ==. The operator for assignment is =.

>How do you print an individual character, not the entire array in C?
Um, don't loop? :icon_rolleyes:

cheers mate!
it works!

the 26 thing i did it on purpose... the assignment operator i just didnt see it... i guess its all about experience!

thanks though i would have had to spend a good couple of hours trying this...

This article has been dead for over six months. Start a new discussion instead.