String encryption
I need some general help with the syntax for an application that 'encrypts' a string using character by character replacement. I want to look for each character using charAt (I think?) and replace it with a character that is not itself, but I am not sure how to write a concise loop that checks each character and returns a different one.
[347 byte] By [
redtaila] at [2007-10-3 8:29:34]

What are you having problems with? It sounds like you know exactly what you need to do.
just the actual syntax of locating the character in array and returning any character but itself, how to get the loop to do that without specifying the replacement for each letter.
Write you own method that accepts a char argument and returns the encrypted char. You can use this method inside your loop.
I know that each char has a value like 52=A,53=B that sort of thing so you need to find out how to get that value and then maybe add your value to it to encrypt the character and then add it back to the string. I'll try to find out exactly what the method is but maybe someone knows and can lend their expertise.
char asciiChar = (char)98;System.out.println(asciiChar);
Thanks, I have to do this without implementing ascii values though. Just replacing each character with another character using a set code. Something like a createNewCode() method that sets the string that will replace the existing characters when in the GUI the user selects a button that runs encode(). I'm just sort of new to putting everything in the right place to get it to run.
What's throwing me off is the code stub that is given is the following:
public String encode(String input) {
StringBuilder builder = new StringBuilder(input);
builder.reverse();
return builder.toString();
}
so I just have to insert createNewCode() in the right place...I think.
anyone see any particular reason this isn't working? I tried them as char arrays as well.There is a GUI that has buttons for each of these methods, but currently it does not return anything when the buttons are pressed and certainly doesn't return an encrypted string.
public class CodeMachine {
//first create class level variables, these will represent alphabet and the code
String alphabet = "abcdefghijklmnopqrstuvwxyz";
String code = null;
public String shuffle(String alphabet) {
int i = 0;
int charLocation = alphabet.charAt(i);
for (i = 1; i < alphabet.length(); i++)
if (alphabet.charAt(i) != code.charAt(i)) {
charLocation = code.charAt(i); //swap with another item
}
i ++;
return alphabet.toString();
}
public String createNewCode() {
//code.toString();
String code = alphabet;
shuffle(code);
return code.toString();
}
//shuffle(code) //now when you adjust a class level object
public String encode (String input) {
int i = 0;
int charLocation = alphabet.charAt(i);
char encryptedChar[] = null;
for (i = 1; i < alphabet.length(); i++) {
charLocation = alphabet.charAt(i);
encryptedChar = code.charAt(i);
}
i++;
return encryptedChar.toString();
}
//for example, say the random code I generated is the alphabet backwards, or
//alphabet = abcdefghijklmnopqrstuvwxyz
//code = zyxwvutsrqponmlkjihgfedcba
//"hello would be encoded into "svool"
public String decode(String input) {
int i = 0;
int charLocation;
int encryptedChar;
String encryptedInput = null;
for (i = 1; i < alphabet.length(); i++) {
charLocation = input.charAt(i);
encryptedChar = charLocation;
}
i++;
return encryptedInput.toString();
}
> public String shuffle(String alphabet) {
> int i = 0;
> int charLocation = alphabet.charAt(i);
> for (i = 1; i < alphabet.length(); i++)
> if (alphabet.charAt(i) != code.charAt(i)) {
> charLocation = code.charAt(i); //swap with
> with another item
> }
> i ++;
> return alphabet.toString();
> }
you do realize that all this code is doing is taking in a string, creating a local int, modifying the int, and returning the string originally sent to the method?
what you might have been trying to do was
public String shuffle(String alphabet) {
int i = 0;
int charLocation = alphabet.charAt(i);
for (i = 1; i < alphabet.length(); i++)
if (alphabet.charAt(i) != code.charAt(i)) {
alphabet[charLocation ]= code.charAt(i); //will store value at i into alphabet at charLocation
//with another item
}
i ++;
return alphabet.toString();
}
I tried a slight variation on the previous suggestion, still think I'm returning the wrong results though:
import java.lang.*;
import java.math.*;
import java.util.*;
public class CodeMachine {
String alphabet = "abcdefghijklmnopqrstuvwxyz";
String code = null;
public String shuffle(String alphabet) {
StringBuilder code = new StringBuilder(alphabet);
int i = 0;
int charLocation = alphabet.charAt(i);
int locationTwo = alphabet.charAt(charLocation);
for (i = 1; i < alphabet.length(); i++) {
if (alphabet.charAt(i) == code.charAt(i)) {
locationTwo = code.charAt(i); //will store value at i into alphabet at charLocation
//with another item
}
}
i ++;
return alphabet.toString();
}
public String createNewCode() {
String coded;
String coder;
StringBuilder newCode;
coded = new String(alphabet);
newCode = new StringBuilder(coded);
coder = newCode.toString();
return coder;
}
public String encode (String input) {
int i = 0;
StringBuilder encrypter = new StringBuilder(input);
encrypter.replace(encrypter.charAt(i), input.charAt(i), input);
return encrypter.toString();
}
public char encrypt(char ch, String input, String builder) {
int index;
char encryptedChar;
boolean isUpperCase = false;
if (Character.isUpperCase(ch)) {
isUpperCase = true;
ch = Character.toLowerCase(ch);
}
if (ch < 'a' || ch > 'z') { //value is greater than or less than value of 26 letter chars
return ch;
}
index = input.indexOf(ch);
encryptedChar = builder.charAt(index);
if (isUpperCase) {
encryptedChar = Character.toUpperCase(encryptedChar);
}
return encryptedChar;
}
public String decode(String input) {
StringBuilder builder = new StringBuilder(input);
int i = 0;
char charLocation;
int encryptedChar;
String encryptedInput;
for (i = 0; i < code.length(); i++) {
charLocation = builder.charAt(i);
encryptedChar = charLocation;
builder.replace(builder.charAt(i), input.charAt(i), input);
}
i++;
return builder.toString();
}
}
