Results 1 to 12 of 12
Thread: Caesar Encrypt and Decrypt
- 01-22-2011, 09:40 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 6
- Rep Power
- 0
Caesar Encrypt and Decrypt
hey guys,
i am absolutely a noob when it comes to programming. at the moment i have got this:
the problem is that only capital letters work. but how do i include low case letters?Java Code:public class CaesarClass { String alphabet ="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; String plaintext; int key; public String encrypt() { StringBuffer buf = new StringBuffer(); for(int i =0; i<plaintext.length(); i++) { char c = plaintext.charAt(i); int index = alphabet.indexOf(c); buf.append(alphabet.charAt((index + key + 26) % 26)); } return buf.toString(); } public CaesarClass(String s, int k ) { plaintext = s; key = k; } }
could you help me? ;)
-
You could change the input String to upper case via a method of the String class. Otherwise you're going to have to put a little more thought into your logic, perhaps have two alphabet variables, one for upper case and one for lower case and an if block to distinguish which one to use, depending on if the input char is upper case or lower case.
- 01-22-2011, 10:01 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 6
- Rep Power
- 0
as i said i havent got a clue what i am doing there :(
could you please correct and add the missing things to my code?
i would really appreciate that. Its nice that you try to help me by teaching me how it works but i cant programme! :((
-
No, that's not how it works here.
We'll try to help you, but you must first be completely clear about what confuses you. This code has some sophisticated stuff in it, in particular the use of mods to shift the char values, and your questions are somewhat basic which leads me to ask, is this your code that you've written, or have you borrowed it from online?i would really appreciate that. Its nice that you try to help me by teaching me how it works but i cant programme! :((
- 01-22-2011, 10:07 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 6
- Rep Power
- 0
i havent borrowed it from someone else. in school we are a group of 2 who are working on this programme. he is good at it and wrote this for me but at the moment he cant finish it because he feels sick..
- 01-22-2011, 10:16 PM #6
Member
- Join Date
- Jan 2011
- Posts
- 6
- Rep Power
- 0
well i know how it would work but i dont know how to write it.
So if the plaintext has small letters use the lower case alphabet and if it has capital ones use the capital ABC.
but how do i write that?
-
Character class has a method isUpperCase that takes a character parameter and returns a boolean true if the character passed in is upper case, and false if lower case. You can use this in an if block to decide which String to use as the alphabet.
- 01-22-2011, 10:25 PM #8
Member
- Join Date
- Jan 2011
- Posts
- 6
- Rep Power
- 0
sorry man, but i cant do that . i will fail then :(
-
What do you mean "you can't do that"? Is it not allowed for you to have an if block in this program?
Or do you mean you don't understand how to create if blocks. If that's the case, then all you have to do is go to the tutorial and read up on them. You can find the Oracle Java tutorial here:
Java Tutorial Really Big Index
If / else tutorial
Please be clear on a point though, that no one here is going to write your code for you. We will try to help you learn, but you are responsible for writing your code, and if you can't do that, then yes you will fail, but if you put in the effort, then no, you won't.Last edited by Fubarable; 01-22-2011 at 10:38 PM.
- 01-22-2011, 10:36 PM #10
Look at this book and its examples. a simple caesar encrypt and decrypt is shown in example Listing4801.java. good luck.
- 01-22-2011, 10:38 PM #11
Member
- Join Date
- Jan 2011
- Posts
- 6
- Rep Power
- 0
public String encrypt() {
StringBuffer buf = new StringBuffer();
for(int i =0; i<plaintext.length(); i++) {
if (small letter ) { then use this alphabet }
char c = plaintext.charAt(i);
int index = alphabet.indexOf(c);
buf.append(alphabet.charAt((index + key + 26) % 26));
}
return buf.toString();
}
you can see that i am an amateur.
-
Similar Threads
-
Caesar Cipher?
By socboy6579 in forum Advanced JavaReplies: 5Last Post: 10-29-2010, 10:59 PM -
Need Help for encrypt/decrypt.
By superdhebz in forum New To JavaReplies: 5Last Post: 09-17-2010, 06:26 PM -
How to decrypt SSL?
By Sergio in forum Advanced JavaReplies: 1Last Post: 08-21-2010, 05:03 AM -
Encrypt and decrypt SMS
By ZeCute in forum CLDC and MIDPReplies: 8Last Post: 06-13-2010, 05:01 AM -
Caesar and encoding with block spaces
By Franneldort in forum New To JavaReplies: 13Last Post: 10-30-2008, 04:48 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks