Task
Given a string, return a string made of the chars at indexes 0,1, 4,5, 8,9 ... so "kittens" yields "kien".
Example test-cases:
altPairs("kitten") → "kien"
altPairs("Chocolate") → "Chole"
altPairs("CodingHorror") → "Congrr"
Solution (to be explained)
String result = "";
/* I understand that I am running
i by 4 to hit 0, 4, 8,12 and all
following ints until the string length ends.*/
for (int i=0; i<str.length(); i +=
4) {
/* why is using
i + 2 is the best way to solve.
this. How is it applicable when
i+1 seems to be 1,
5, 9, 13 etc.*/
int end = i + 2;
if (end > str.length()) {
end = str.length();
}
result = result +
str.substring(i, end);
}
return result;
}