This is not in my head, but one method that I try will be like this:
Find the index of my middle character in the array. Call it "startIndex"
For each line, I will find the length, divide it by 2 (round down), and this will be the initial offset from startIndex, defining my first character.
Let's say I'm on line 5, which you have as "Z ANIVERS". This has a length of 9. Divide by 2 (and round down), you get 4.
In the chars array, the value of startIndex is 8 (17/2, rounding down). So, I have to start typing from char 4 (which is startIndex - 4) in my char array up to any number of characters.
You can also easily print the entire line without counting the characters in the loop, since if you know the required line length and the starting char index, you can simply fine-tune this line at a time if you had the starting characters as one line.
Finally, you can only do half the work, since it looks like your upper half is the same as the lower half (create the result in rows, then combine them together to print the full result)
edit Reading my answer, you would not really have to βfindβ the length of the string, since it only starts at 1 and increments by 2 each time, but the original method will still be the same.
change 2:
As each one adds its own encoded version (and the answer was accepted), I could also add my next one, which I said:
String sourceString = "FELIZ ANIVERSARIO"; // source of characters String padding = " "; // used to create the padding offset for each line int middle = sourceString.length() / 2; // top and bottom halves of the output String top = ""; String bottom = ""; for(int currentLength = 1; currentLength < sourceString.length(); currentLength += 2){ String linePadding = padding.substring(currentLength / 2, padding.length()); // speical case here, in the given example by the post, the 4th line the offset is one less as the I characters do not line up in the middle // 7 is used as the 4th line has a length of 7 if(currentLength == 7){ linePadding = linePadding.substring(1, linePadding.length()); } top += linePadding + sourceString.substring(middle - (currentLength / 2), middle + (currentLength / 2) + 1) +"\n"; bottom = linePadding + sourceString.substring(middle - (currentLength / 2), middle + (currentLength / 2) + 1) + "\n" + bottom; } System.out.println(top + sourceString +"\n"+ bottom);
Not sure if this was intentional, but I added a special expectation in the loop, where I noticed in the user example that the 4th line is disabled by 1 (I am not lining up there). If I have to line up and it was a typo, you can remove it if the block is complete.