I was given a new assignment. I'm supposed to write a version of Caesar's cipher. It's been close to a week since I've started working on this and I'm nowhere closer than I was when I started.
I tried defining every letter in the alphabet and assigning it a number but then I got stuck at making the 'program' code and decode. I thought I might have to use an array but I have no clue on how to implement that.
int count = 0, length;
cout << "Enter your phrase: \n";
length = (int)input.length();
for (count = 0; count < length; count++)
input[count] = tolower(input[count]);
for (int i = 0; i < 5; i++)
if (input[count] == 'z')
input[count] = 'a';
cout << "Results: \n" << input <<endl;
The code works perfect for encrypting. While I also have to decrypt I think I'll manage to figure that out once I understand step 1: encrypting.
So, as I said, the code works, but I don't understand how. How does it do its magic? I just don't get it. How does it know that if I input 'a', that 'a' = 0? How does it know that if I want it to shift by 4, that 0 + 4 = 4 and that 4 corresponds to 'd'? What is 'isalpha'? What does 'input[count]' mean? And what in the world is 'tolower' ('tolower(input[count])'. Since none of them are defined anywhere and the compiler gives no errors I suppose they're some sort of built-it functions unknown to me?
I really hope somebody can make me understand so I can finally write my version of this :(
Thank you so much for your answer! It sure gave me a better understanding of what I'm supposed to do!
I've written my code, submitted it to my course's page but I got a whole lot of testing failures xD. At least I have a code to work with and I'm slowly understanding what's wrong with it and how to fix it!
I would dare to recommend write this program from scratch rather than using some other's code found in google. It is quite wrong way of learning as it does not give you understanding of the problem usually.
Shortly speaking to encrypt the letter you should do the following:
- convert it to 'int' value (simply by assigning char to int);
- subtract 'A' (so that 'A' becomes 0, 'C' becomes 2, 'Z' becomes 25 etc.);
- now add your key (value from 1 to 25);
- if the result is 26 or higher, subtract 26 (to wrap around alphabet);
- add 'A' again (so 0 becomes 'A', 2 becomes 'C', 25 becomes 'Z');
- and now you have an encrypted letter.
To decrypt the message which was encrypted with key K simply encrypt it once again but with the key '26 - K'.
Here is the task at my site which you can use to practice and test your solution.
While I agree that figuring something out on your own can be more helpful than using someone else's work, I strongly disagree that spending too much time figuring that something out on your own without result is of any help. It's just a waste of time and a frustration builder.
Sometimes you need to see someone else's work to understand what's going on.
For example I had two assignments 3 weeks ago which I just couldn't figure out, nobody was willing to give me even the beginning of a code and I couldn't find anything useful over the internet. Every day since then I've been trying to write those programs but still no good. If someone had showed me their code, I would have understood it in a matter of minutes and then I would have been able to write my own code. But no, now I'm left with two programs I will probably be able to solve when I have months of experience and by then I'll probably even forget about them.
Like the code above. coder777 was kind enough to explain things I didn't understand and things I didn't even know existed. I worked with that code, understood it and guess what? By fooling around with it I saw the faults in it. I understood what was wrong with it and wrote the program from scratch. The code I wrote is very different from the one posted above.
I don't know if it's the most 'clean' and pretty Caesar cipher code out there, but it works 100%, unlike many I've found on Google and it gave me perfect score for my class.
I'm just saying that there are different types of learning, people learn the best in different ways. I think it's more efficient for me the way I did it. Before I found that code I spent almost a week and had no idea where to even begin, even though theoretically knew what I had to do, but I didn't know how to translate it from words int c++ language. Working with someone else's code gave me better understanding of the problem, got my mind working and came up with my very own code in less than 5 hours with breaks.
Some are born geniuses and can just do things on their own, others aren't born with that spark and need teachers and examples to learn things. Unfortunately I fall under the second category.