So the question is to write a java program that will get a String and a Character from input and it will encode it using a method like this:
Encoding method: the method will calculate the distance between the input character and the first letter of input string and add the input character to the beginning of that string and it will shift the rest of the letters of input based on the distance number.
example1: String --> Ali & Char --> D
Distance between D and A --> 3
Result: DAol
example2: String --> Test & Char --> R
Distance between R and T --> -2
Result: RTcqr
The program should also be able to decode the input.
example1: DAol --> Ali
example2: RTcqr --> Test
I tried this for Encoding but it didn't work.
public static void encode() {
String text = "Ali";
String result1 = "";
String result2 = "";
String finalResult = "";
char add = 'D';
char[] chars = {'a', 'b' ,'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
int dis = add - result1.charAt(0);
result2 += add;
result2 += result1;
for (int i = 0; i < result2.length(); i++) {
if (i < 2) {
finalResult += result2.charAt(i);
} else {
int temp = result2.charAt(i) + dis;
System.out.println("temp: "+temp);
finalResult += chars[i];
}
}
System.out.println(dis);
System.out.println(finalResult);
}
Alipasses to beDAol. Why theo?