Text pyramid using recursion in Java

I am trying to make a complete triangle with any text input. Example, if I have a string that is "abcdefghij", I want the result to be

aj abij abchij abcdghij abcdefghij 

if the length of the string is odd, as in "abcdefghij", then the output will be

  a abi abchi abcdghi abcdefghi 

Here is what I have so far, but my conclusion for words has turned upside down. My result

  abcdefghij abcdghij abchij abij aj 

What have i done so far

 public static void main(String[] args) { solve("abcdefghij"); } public static void solve(String word) { solve(word, word.length()/2-1); } public static void solve(String word, int it) { // print starting spaces for(int i = 0; i < it; i++) System.out.print(" "); // print out string System.out.print(word+"\n"); if(word.length() > 2) { int newlengthperside = (word.length() - 2)/2; solve( word.substring(0, newlengthperside) + word.substring(word.length() - newlengthperside), it-1); } } 

I just need to ask how to start with aj and not finish. Thanks for any help. This is homework, so only a hint is recommended.

+5
source share
2 answers

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) { // print starting spaces String spaces=""; for(int i = 0; i < it; i++) spaces+=" "; if(word.length() > 2) { int newlengthperside = (word.length() - 2)/2; solve(word.substring(0, newlengthperside) + word.substring(word.length() - newlengthperside), it + 1); } System.out.print(spaces+word+"\n"); } 

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+=" "; 
  1. solve (word, 0); // -> 0 of length

  2. 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 
+2
source

Change the string "// print out string" to your recursive call:

 public static void solve(String word, int it) { if(word.length() > 2) { int newlengthperside = (word.length() - 2)/2; solve( word.substring(0, newlengthperside) + word.substring(word.length() - newlengthperside), it-1); } // print out string System.out.print(word+"\n"); } 

First, the shortest string will be displayed, then when the function returns, the next largest word will be displayed, etc. up the chain. You will need to execute part of the space yourself, but this should give you a start (I think that the existing loop located between the recursion areas and "// print out string" will work).

0
source

Source: https://habr.com/ru/post/1237127/


All Articles