You should look like this:
public void solve(String str) { for(int i=1;i<=str.length()/2;i++) { for(int j=str.length()/2-i; j>0 ;j--) { System.out.print(" "); } System.out.print(str.substring(0,i)); System.out.print(str.substring(str.length()-i)); System.out.println(); } }
Entrance:
"abcdefghij"
Output:
aj abij abchij abcdghij abcdefghij
It only covers a happy journey, but you understand the logic.
EDIT:
For a recursive approach:
public static void solve(String word) { solve(word, 0); } public static void solve(String word, int it) {
I changed a few things:
1. counting the number of spaces needed and putting them in a line that is used later.
String spaces=""; for(int i = 0; i < it; i++) spaces+=" ";
solve (word, 0); // -> 0 of length
solve (word.substring (0, newlengthperside) + word.substring (word.length () - newlengthperside), it + 1); // -> adding 1 to the length
Entrance:
solve("abcdefghij");
Output:
aj abij abchij abcdghij abcdefghij
source share