Printing a pyramid of symbols

I need to print an array of characters in the shape of a pyramid. What I'm still like this:

char[] chars = {'F', 'E', 'L', 'I', 'Z', ' ', 'A', 'N','I', 'V', 'E', 'R', 'S', 'A', 'R', 'I', 'O'}; int length = chars.length; for (int i = 1; i < length; i += 2) { for (int j = 0; j < 9 - i / 2; j++) System.out.print(" "); for (int j = 0; j < i; j++) System.out.print(chars[j]); System.out.print("\n"); } for (int i = length; i > 0; i -= 2) { for (int j = 0; j < 9 - i / 2; j++) System.out.print(" "); for (int j = 0; j < i; j++) System.out.print(chars[j]); System.out.print("\n"); 

and he prints this:

  F FEL FELIZ FELIZ A FELIZ ANI FELIZ ANIVE FELIZ ANIVERS FELIZ ANIVERSAR FELIZ ANIVERSARIO FELIZ ANIVERSAR FELIZ ANIVERS FELIZ ANIVE FELIZ ANI FELIZ A FELIZ FEL F 

But I need it to start typing with a character in the middle of the array. The end result should be like this:

  I NIV ANIVE ANIVER Z ANIVERS IZ ANIVERSA LIZ ANIVERSAR ELIZ ANIVERSARI FELIZ ANIVERSARIO ELIZ ANIVERSARI LIZ ANIVERSAR IZ ANIVERSA Z ANIVERS ANIVER ANIVE NIV I 

Any help would be greatly appreciated.

+5
source share
4 answers

You can do the same as show the code below. It will use fewer cycles. I added inline comments to the code to go through it.

  char[] chars = { 'F', 'E', 'L', 'I', 'Z', ' ', 'A', 'N', 'I', 'V', 'E', 'R', 'S', 'A', 'R', 'I', 'O' }; int length = chars.length; for (int line=0;line<=length;line++) { //This will print upper part of pyramid if(line < length/2){ String output=""; int middelVal=length/2; for (int i = middelVal - line; i > 0; i--) { output=output+" "; } for (int i = middelVal - line; i <= middelVal + line; i++) { output=output+chars[i]; } System.out.println(output); }else if(line > length/2){ //This will print lower part of pyramid String output=""; int middelVal=length/2; int nwNum = chars.length-line; for (int i = middelVal - nwNum; i > 0; i--) { output=output+" "; } for (int i = middelVal - nwNum; i <= middelVal + nwNum; i++) { output=output+chars[i]; } System.out.println(output); } } 
+5
source

Obtaining an average index, storing it in two variables, will mean where to start and end character printing.

 int mid = length / 2; // left end int midLen = mid; // right end 

The loop will start printing spaces to the left end of mid , then characters will be printed to the right end of midLen + 1 . Then increment midLen to go further to the right, and decrease mid to go further to the left.

 for(int i = mid; mid > -1; mid--, midLen++) { for(int s = 0; s < mid; s++) System.out.print(" "); for (int j = mid; j < midLen + 1; j++) { System.out.print(chars[j]); } System.out.print("\n"); } 

To print the bottom pyramid, we start from 0 index to length . This time we increase mid go further to the right and decrease midLen to go further to the left, truncating the line of the line by line to the middle character.

 mid = 0; midLen = length; for(int i = mid; midLen != length / 2; mid++, midLen--) { for(int s = 0; s < mid; s++) System.out.print(" "); for(int j = mid; j < midLen; j++) { System.out.print(chars[j]); } System.out.print("\n"); } 

Check here

BONUS: Using Java 8 new features . The same mechanism, I will leave a consideration for you.

 ... static int mid; static int midLen; static String line = ""; public static void main(String[] args) { char[] chars = {'F', 'E', 'L', 'I', 'Z', ' ', 'A', 'N','I', 'V', 'E', 'R', 'S', 'A', 'R', 'I', 'O'}; mid = chars.length / 2; midLen = mid; List<String> tri = new ArrayList<String>(); IntStream.rangeClosed(0, mid) .forEach(i -> { Stream.of(chars).forEach( j -> { IntStream.range(0, mid).forEach(x -> line += " "); IntStream.rangeClosed(mid, midLen) .forEach(x -> line += String.valueOf(chars[x])); mid--; midLen++; tri.add(line); line = ""; }); }); tri.forEach(System.out::println); // print upper pyramid Collections.reverse(tri); tri.forEach(System.out::println); // print lower pyramid } ... 
+3
source

Here is another approach using a 2d array

  char[] chars = { 'F', 'E', 'L', 'I', 'Z', ' ', 'A', 'N', 'I', 'V', 'E', 'R', 'S', 'A', 'R', 'I', 'O' }; int size = chars.length; int mid = size / 2 + 1; char[][] matrix = new char[size][size]; for (char[] cs : matrix) { Arrays.fill(cs, ' '); } for (int i = 0; i < size; i++) { int l = Math.abs(mid - 1 - i); int r = size - l; for (int m = l; m < r; m++) { matrix[i][m] = chars[m]; } } for (char[] cs : matrix) { System.out.println(cs); } 

Output

  I NIV ANIVE ANIVER Z ANIVERS IZ ANIVERSA LIZ ANIVERSAR ELIZ ANIVERSARI FELIZ ANIVERSARIO ELIZ ANIVERSARI LIZ ANIVERSAR IZ ANIVERSA Z ANIVERS ANIVER ANIVE NIV I 
+3
source

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.

+2
source

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


All Articles