Darya Rioux
Darya Rioux

Reputation: 29

Java function charAt

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.

Upvotes: 2

Views: 125

Answers (1)

Sajjad
Sajjad

Reputation: 3228

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)

Upvotes: 1

Related Questions