Basically I’m looking to take a string and just alternate between front and back. For instance, let’s say I have the following string.
Android
That would then be output like this:
adnidor
It would alternate between the front and back
First letter `a`
Last letter `d`
Second letter `n`
Second to last `i`
etc.
to give `adnidor` in the end
How could something like this be done?
=======
The final solution went like this:
String r = "";
String s = "android";
int i = 0;
int j = s.length() - 1;
while (i < j) {
r += s.charAt(i++);
if (i < j) {
r+= s.charAt(j--);
}
}
if (s.length() % 2 == 0) {
int l = (s.length() / 2) - 1;
int f = l + 1;
r = r + s.charAt(f);
}
else {
int l = ((s.length()) / 2);
r = r + s.charAt(l);
}
1 Answer