Hello all! I have been doing the pythonprogramming challenges in ruby ( i will do them again in python, i want to get better with ruby first and i am noticing it helping me a lot) Well, one of the levels required us to do some cryptograms. Pretty much just shift the letter/number over by 2. I started doing it but then I feel the need to add more to it, So I have it shift as many times as the user wants! It works great! But I have some errors which I have been trying to fix, but I feel as if I have reached the end of my road. So can you guys help me out? Ok well first of all, I would like you to tell me if my code could be improved. I like to know better ways to program, after all, that's how one gets better...critics. Secondly, when I shift and it goes past "z", I get "aa". Is there a way to just loop it to "a"? Thirdly, is there a way( the way I programmed it, I couldn't think of a way to retain special characters like " " or ".") to preserve special characters? Here is my code:
CODE
def encrypt(sentence, incr)
hld = []
ct2 = 0
ct = incr
sentence.each_byte do #put each character into the array hld[] and convert it back to ascii
|c| hld.push(c.chr)
end
# print(hld)
#test for string print
while ct2 < ct
h =hld.each{|c| c.succ!}
ct2+=1
end
print(h)
end
encrypt("test stringgggzzz", 4)
And another small question, sorry for so many! When performing this code in netbeans, Nothing happens in my output box, it kind of just hangs unless I spam the redo exec button. But if I do it in Geany (lightweight IDE) it works fine. Does it have to do with my code?
Thank you in advance!