Maksym Fedenko
Maksym Fedenko

Reputation: 3

How to overwrite String in loop

public static String decrypt(final String encryptedText, final int n) {


    char [] chars;
    String s = encryptedText;
    String s1 = "";
    int buffer = 0;
    int buffer2 = 1;

    for (int j = 0; j < n; j++) {
        if (j < 1){
            chars = s.toCharArray();
        }else{
            chars = s1.toCharArray();
        }
        char [] charsUpdate = new char[chars.length];

        for (int i = chars.length / 2; i < chars.length; i++) {
            if (buffer % 2 == 0 && buffer <= charsUpdate.length){
                charsUpdate[buffer] = chars[i];
            }
            buffer += 2;
        }
        for (int i = 0; i < chars.length / 2 ; i++) {
            if (buffer2 % 2 != 0 && buffer2 < charsUpdate.length){
                charsUpdate[buffer2] = chars[i];
            }
            buffer2 += 2;
        }
        s = "";
        s1 = "";
        for (int i = 0; i < charsUpdate.length; i++) {
            s = s + charsUpdate[i];
        }
        s1 = s;
    }
    return s;

Hi, community. I have some problem here. I try to resolve some task and have stuck in moment when I need to overwrite my String in loop. I need give my old String in start of loop and overwrite this String in end of loop and give new String to start of loop, something like that, but I can't do this, because after first iteration in the start of loop I can see my empty String. Sorry for my shit-code and bad english :)

Upvotes: 0

Views: 129

Answers (1)

Nowhere Man
Nowhere Man

Reputation: 19565

There are some other issues:

  1. ArrayIndexOutOfBoundsException because the condition buffer <= charsUpdate.length is incorrect
  2. Variables buffer and buffer2 are not reset at the end of the loop, so after recreation of charUpdate array during the 2nd and the following iteration the symbols are not copied from chars to charUpdate

Updated and refactored code may look as follows:

    public static String decrypt(final String encryptedText, final int n) {
        int len = encryptedText.length();
        int half = len / 2;
        String s = encryptedText;

        for (int j = 0; j < n; j++) {
            char[] chars = s.toCharArray();

            char[] charsUpdate = new char[len];

            for (int i = half, even = 0; i < len && even < len; i++, even += 2) {
                charsUpdate[even] = chars[i];
            }
            for (int i = 0, odd = 1; i < half && odd < len; i++, odd += 2) {
                charsUpdate[odd] = chars[i];
            }
            s = new String(charsUpdate);
        }
        return s;
    }

But I am not sure that it produces valid results.

A simple test shows the following output:

for (int i = 1; i < 5; i++) {
    System.out.println("abcdefgh (" + i + ") -> " + decrypt("abcdefgh", i));
}

output:

abcdefgh (1) -> eafbgchd
abcdefgh (2) -> gecahfdb
abcdefgh (3) -> hgfedcba
abcdefgh (4) -> dhcgbfae
----
// for odd length of the input string
abcdefg (1) -> daebfcg
abcdefg (2) -> bdfaceg
abcdefg (3) -> abcdefg
abcdefg (4) -> daebfcg

Upvotes: 1

Related Questions