Reddit Programming Challenge #3
Iām a long time follower of tinyjavacodeĀ and Iāve watched them turn out a daily snippet of code solving each of the reddit programming challenges. So, figure Iād give it a shot. Todayās challenge was to create a substitution cypher. Tinyjavacode did this already in java so I did it in c.Ā
Check under the break to see the code and for a breakdown of it.
#include <stdlib.h> #include <stdio.h> #include <string.h> char * encryptSubstitutionCypher(char *inputText); char * decryptSubstitutionCypher(char *inputText); int main(){ char testText[] = "Hello World."; printf("Original Text\t: %s\n", testText); char * encryptedText = encryptSubstitutionCypher(testText); printf("Encrypted Text\t: %s\n", encryptedText); char * decryptedText = decryptSubstitutionCypher(encryptedText); printf("Decrypted Text\t: %s\n", decryptedText); } char * encryptSubstitutionCypher(char inputText[]){ char * returnValue; returnValue = malloc(sizeof(char) * strlen(inputText)); strcpy(returnValue, inputText); int i; for(i = 0;returnValue[i] != '\0';i++){ returnValue[i] = returnValue[i]+5; } return returnValue; } char * decryptSubstitutionCypher(char inputText[]){ char * returnValue; returnValue = malloc(sizeof(char) * strlen(inputText)); strcpy(returnValue, inputText); int i; for(i = 0;returnValue[i] != '\0';i++){ returnValue[i] = returnValue[i]-5; } return returnValue; }
Original Text : Hello World. Encrypted Text : Mjqqt%\twqi3 Decrypted Text : Hello World.
C made this encryption/decryption code pretty simple. Char values are already stored as ascii values so I took those values and added 5 to them to encrypt them. Then did the opposite to decrypt them. This has the added bonus of encrypting special characters too.
The encrypt and decrypt functions are identical except the +5 and =5 in the for loop. Step by step,
We declare our returnValue as a char pointer, which has the added benefit of acting like an array if you know how to play with it.
Allocate the memory needed to store the entire input string in our return String.
Copy the input string to our return string character for character.
Declare our iterator i.
Loop through the characters in the return string adding 5 to the ascii value of each one.
Then return the value to our driver.
All in all this was a fun program to cobble together. Biggest problem I had was allocating the memory. I forgot to do that originally and was segfaulting before I realized the mistake.
Now a comparison to tinyjavacodeās solution. You can be find itĀ here. It should also be noted I havenāt read their description of the code, nor have I looked at their code till now.
For the encryption portion it looks like they iterate through the alphabet until they find the letter that matches the first letter of the message to be encoded. Then we use that first letter as the first part of our substitution cypher.
Only thing Iād change here is that first iteration. I think instead of the for loop we could use something like:
cypher = alphabet.lastIndexOf(message.charAt(0).charValue());
Problem with the above is your lose readability but you gain speed.
Next up is the decryption portion which goes beyond what I did. My version only decrypts one version which is to move the text -5. tinyjavacodeās version of the decryption has to be able to work with multiple versions of encryption since the encrypt code can encrypt in multiple ways.Ā
Overall this was fun to work on, and I look forward to more challenges. I tip my hat to tinyjavacode for being more creative with their encryption and decryption.













