Can anyone tell me what this snippet of code does? In my understanding it returns the sum of the indexes in the string, for example, in string is "Hello"
it would return 10? AM I wrong here, please let me know…
public String decrString(final String s) {
final char[] value = new char[s.length()];
for (char index = '\0'; index < s.length(); index) {
value[index] = s.charAt(index);
if (index % '\u0002' == 0) {
value[index] -= index;
}
else {
value[index] = index;
}
}
return new String(value);
}
I googled charAt
- it simply returns the current index. Java isn't my strongest, first time seeing Java code.
CodePudding user response:
Seems that you are missing the encryption method that pairs with the method you posted! I have both encrypt and decrypt methods below commented out line by line. I hope this helps!
public static void main(String[] args) {
Encrypt encrypt= new Encrypt();
String testOne = encrypt.encrString("Hello Stack Overflow User");
String testTwo = encrypt.encrString("Welcome to java");
String testThree = encrypt.encrString("Hope you are doing well");
System.out.println(testOne);
System.out.println(testTwo);
System.out.println(testThree);
System.out.println(encrypt.decrString(testOne));
System.out.println(encrypt.decrString(testTwo));
System.out.println(encrypt.decrString(testThree));
}
/**
* If the index is even, subtract the index from the character, otherwise add the index to the character.
*
* @param s The string to be decrypted.
* @return The decrypted string.
*/
public String decrString(final String s) {
final char[] value = new char[s.length()]; // allocate array of same length as string to be decrypted
for (char index = '\0'; index < s.length(); index) { // loop through string
value[index] = s.charAt(index); // get char at index
if (index % '\u0002' == 0) { // if index is even
value[index] -= index; // subtract index from char
}
else { // if index is odd
value[index] = index; // add index to char
}
}
return new String(value); // return new string
}
/**
* If the index is even, subtract the index from the char, otherwise add the index to the char.
*
* @param s The string to be encrypted
* @return A string
*/
public String encrString(final String s) {
final char[] value = new char[s.length()]; // allocate array of same length as string to be decrypted
for (char index = '\0'; index < s.length(); index) { // loop through string
value[index] = s.charAt(index); // get char at index
if (index % '\u0002' == 0) { // if index is even
value[index] = index; // subtract index from char
}
else { // if index is odd
value[index] -= index; // add index to char
}
}
return new String(value); // return new string
}
The Output looks like this
HdnisYmiZu[iscv[d4@N
Wdn`shk|f*_mio
Hnrb$tun(X|Z,W}Z~V2dyW
Hello Stack Overflow User
Welcome to java
Hope you are doing well
CodePudding user response:
this is kind of Cryptography function and changes a string to an encoded String.(ciphertext).
How it Work?
let's look at some input and output
System.out.println(decrString("AAAAAAAAAAAA"));
AB?D=F;H9J7L
System.out.println(decrString("111111111111"));
12/4-6 8):'<
System.out.println(decrString("999999999"));
9:7<5>3@1
System.out.println(decrString("55555"));
56381
it starts from first of String and if it place is odd it adds [place index] and if it place is even is mines [place index]
look at first example you can see AB D F H J L
it generated with this formula (A-0=A)(A 1=B)(A 3=D)(A 5=F)(A 7=H)...
look at third example you can find this 9 7 5 3 1
it generated such this
(9-0=0)(9-2=7)(9-4=5)(9-6=3)(9-8=1)
look at forth example it is 56381
and generated like this
(5-0=5)(5 1=6)(5-2=3)(5 3=8)(5-4=1)