Your copying logic is incorrect. You need to copy the letter from index i
to index 2*i
and 2*i + 1
. The i++
at the end is unnecessary; it's already done in the for
loop. Change
charArray[i] = string.charAt(i);
charArray[i+1] = charArray[i];
i++;
to
charArray[2*i] = string.charAt(i);
charArray[2*i+1] = string.charAt(i);