r/dailyprogrammer • u/nottoobadguy • Feb 11 '12
[2/11/2012] Challenge #3 [easy]
Welcome to cipher day!
write a program that can encrypt texts with an alphabetical caesar cipher. This cipher can ignore numbers, symbols, and whitespace.
for extra credit, add a "decrypt" function to your program!
27
Upvotes
6
u/iostream3 Feb 11 '12
4
u/robosatan Feb 11 '12
Python 2.7
4
u/gitah Feb 11 '12
Wrote a python function:
4
u/m_geist Feb 12 '12 edited Feb 12 '12
I wanted to plug this into a graphical interface, because it's been something I've been meaning to do. The shift code itself is not my best work, but overall I like how it turned out. Used PyQt4 to do the interface, not bad once I got the hang of it. It's got a good deal of duplicated code, but I wanted it to handle uppercase and lowercase, and keep case after translation.
Code can be found here: https://github.com/mgeist/CaesarShift
Oh, and before anyone sees my .gitignore and wonders wtf is going on, I'm trying out the Python tools for Visual Studio. Pretty nifty stuff.
3
u/garslo Feb 11 '12 edited Feb 11 '12
Common lisp:
3
u/Crystal_Cuckoo Feb 12 '12 edited Feb 12 '12
I wrote a rather wordy one in Python:
But looking through the answers already given you can just use:
and it'll shift the message across 13 characters. Damn, Python really does make everything easier.
3
u/philcannotdance Feb 13 '12
Awesome you're using my idea! Thanks :D
I'll try this soon.
2
u/stiggz Feb 11 '12 edited Feb 11 '12
Simple jquery solution
2
u/robin-gvx 0 2 Feb 11 '12
Déjà Vu is pretty fun -- not as easy as Python, though.
http://hastebin.com/raw/temoxiyaya
2
u/iostream3 Feb 11 '12 edited Feb 11 '12
Slightly better version than my previous:
Usage:
"nopNOP"
2
u/wikirobot Feb 11 '12 edited Feb 11 '12
A slightly better version then my previous. Still not great.
2
u/classicrockielzpfvh Feb 11 '12
In python: http://sprunge.us/cCUG?python
Usage: ./script_name <number> <msg>
2
u/drb226 0 0 Feb 11 '12
12 lines of Haskell: http://hpaste.org/63488
Will explode if you give it weird unicode characters that satisfy
isAlpha.2
u/FataOne 0 0 Feb 12 '12
My solution in C: http://pastebin.com/WrVVkr47
2
u/CachooCoo Feb 12 '12 edited Feb 12 '12
C++. Still in my early learning phases. Any obvious improvements I can make (readability, organization, etc)?
The function to decrypt:
2
u/lnxaddct Feb 12 '12
Python solution: https://gist.github.com/1806459
It defines two functions (encode/decode) and uses python's built-in rot13 encoding.
2
u/flying_petunia Feb 12 '12
Hi! This is my take in Perl. I don't like those nested loops, so if anyone has any comments or improvements, they would be much appreciated. I didn't focused on input parameters, since that isn't the focus of the challenge. Also, to decrypt, just use the negative key (or the positive, if your original key was negative).
2
u/jnaranjo Feb 16 '12
http://pastebin.com/nx9Wvn2T Hope I'm not too late for the party!
Written in 100% python. Can encrypt and decrypt (files or strings). You can specify the "key" (number of places to shift the alphabet).
Also can handle command line arguments for behaviour and key values.
2
u/driadan Feb 17 '12
using the challenges to help myself learn scala. Here's mine: http://pastebin.com/aRD0AAS3
2
u/mymainmanbrown Feb 21 '12
Here is a python implementation i haven't seen yet:
I used this specifically because the if/elif/else structure only runs once per word (separated by a space) and it not so elegantly handles a value that is not decrypt or encrypt.
2
u/PrivatePilot Feb 22 '12
Ruby
1
u/foriamraindog Feb 20 '12
My Java attempt:
1
u/ginolomelino Mar 18 '12
Fairly comprehensive Javascript implementation using Unicode to preserve capitalization without the need for multiple (or any) arrays of alphabet characters. Includes a decrypt function for extra credit. The math was giving me trouble. I'm sure there's an easier way to do it. Suggestions appreciated.
1
u/savagecub Apr 10 '12
C++ somewhat embarrassed to submit this but its the best i could do without copying other peoples code. does not work with spaces but, and all it really does is reverse the order of the chars but it technically encrypted it. any input would be very welcome
include "stdafx.h"
include <iostream>
include <string>
using namespace std;
int main() {
}
1
u/MadSoulSK May 02 '12 edited May 02 '12
http://pastebin.com/iYmjWyAa
C# Console program, mostly reads from standard input and writes to standard output. You can specify also the step to cypher (example: step 1 is a -> b, m -> n; step 2 is a -> c, m -> o and so on) output and input and also can decrypt if you know the right step.
Console arguments: -i FileName = File to be encrypted. If this argument is not used it will use standard input -o FileName = File where to write encrypted/decrypted text. If this argument is not used, it will use standard output -s integer = size of step to cypher. Default is 1 -d = Decrypt instead of encrypt
example of usage: program.exe -i original.txt -o encrypted.txt
program.exe -encrypted.txt -o decrypted.txt -d
arguments arent checked so watchout for exceptions...was lazy
It can encrypt lower, upper, digit and some symbols.
Edit: the program ends encrypting whem it finds empty line, also forgot to properly close streams... but isnt a disaster
1
u/[deleted] Jun 30 '12
Had one of these lying around from a class I took. Python 3.0:
1
u/[deleted] Jul 20 '12
Do i need more comments?
1
u/ragtag_creature Jul 09 '22
R - fortunately there is a Caesar library in existence which makes things easier. Attempted a menu to walk user through the process