Triangular word pattern using nested loops in Java

For this purpose, after entering a word, it will print it according to the template shown below (in this case, this word is a computer):

       C
      O O
     M   M
    P     P
   U       U
  T         T
 E           E
RETUPMOCOMPUTER

Currently my code is this:

    String output = "";
        for (int a = word.length()-1; a >= 1; a--)
        {
            for (int b = 0; b < word.length(); b++)
            {
            out.print(" ");
            }
        out.println(word.charAt(word.length()-1-a));
        }

        for (int c = 0; c < word.length(); c++)
        {
            out.print(word.charAt(word.length()-1-c));  
        }
        out.print(word.substring(1));   
    return output + "\n";

Currently the output for my code is:

        C
        O
        M
        P
        U
        T
        E
RETUPMOCOMPUTER

Any advice or advice is greatly appreciated, thanks in advance!

+4
source share
4 answers

The logic is simple, first try creating the last line using reverseof StringBuilder. Then print each line from the first to the last line.

The case last lineis simple.

first last line - 1 , , 0, 1, 2..., .

    public void printTriangle(String input) {
        String tmp = input.substring(1);//Take the suffix
        StringBuilder builder = new StringBuilder(tmp);
        builder = builder.reverse().append(input);//Reverse, then append it
        String line = builder.toString();//This is the last line
        for(int i = 0; i < input.length(); i++){
            for(int j = 0; j < line.length(); j++){
                //Print the last line, or those that have distance equals i to the center of the last line
                if(i + 1 == input.length() || Math.abs(j - line.length()/2) == i){
                    System.out.print(line.charAt(j));
                }else{
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }

COMPUTER

       C       
      O O      
     M   M     
    P     P    
   U       U   
  T         T  
 E           E 
RETUPMOCOMPUTER

STACKOVERFLOW

            S            
           T T           
          A   A          
         C     C         
        K       K        
       O         O       
      V           V      
     E             E     
    R               R    
   F                 F   
  L                   L  
 O                     O 
WOLFREVOKCATSTACKOVERFLOW
+2

, , . , :

public static void printTriangle(String str){       
    int len = str.length()-1, idx = 1;

    System.out.println(String.format("%"+(len+1)+"s", str.charAt(0)));
    for(int x=0; x<str.length()-2; x++){
        System.out.print(String.format("%"+(len--)+"s", str.charAt(idx)));
        System.out.println(String.format("%"+(idx*2)+"s", str.charAt(idx++)));
    }
    System.out.println(new StringBuilder(str.substring(1)).reverse().toString() + str);
}

:

       C
      O O
     M   M
    P     P
   U       U
  T         T
 E           E
RETUPMOCOMPUTER
+1

, , , , :

String someString = "COMPUTER";
    switch(someString.length()) {
    case 0: System.out.println();
            break;
    case 1: System.out.println(someString);
            break;
    default:
        int _spaces_before_after = someString.length()-1;
        int _spaces_middle = 0;
        for(int i=0; i<someString.length(); i++){
            if(i!=someString.length()-1){
                if(i==0){
                    for(int j=0; j<_spaces_before_after; j++)
                        System.out.print(" ");
                    System.out.print(someString.charAt(0));
                    for(int j=0; j<_spaces_before_after; j++)
                        System.out.print(" ");
                    System.out.println();
                    _spaces_middle = 1;

                }
                else {
                    for(int j=0; j<_spaces_before_after; j++)
                        System.out.print(" ");
                    System.out.print(someString.charAt(i));
                    for(int j=0; j<_spaces_middle; j++)
                        System.out.print(" ");
                    System.out.print(someString.charAt(i));
                    for(int j=0; j<_spaces_before_after; j++)
                        System.out.print(" ");
                    System.out.println();
                    _spaces_middle+=2;
                }
                _spaces_before_after-=1;
            }
            else {
                for(int j=someString.length()-1; j>=0; j--)
                    System.out.print(someString.charAt(j));
                for(int j=1; j<someString.length(); j++)
                    System.out.print(someString.charAt(j));
            }
        }
        break;
    }
0

, str n. n & times; 2n+1.

c , n

(str[0]) .

(str[1]) c-1 c+1.

(str[2]) c-2 c+2.

.

. c, str. .


- , :

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String str = br.readLine();

int n = str.length();

char[][] matrix = new char[n][2*n+1];

char[] chrArr = str.toCharArray();

// initializes the matrix with blank spaces
for (int i = 0; i < n; i++) {
    for (int j = 0; j < 2*n+1; j++) {
        matrix[i][j] = ' ';
    }
}

// build the two sides of the triangle
for (int i = 0; i < n - 1; i++) {
    matrix[i][n-i] = chrArr[i];
    matrix[i][n+i] = chrArr[i];
}

// last line, build the base of the triangle
for (int i = 0; i < n; i++) {
    matrix[n-1][n-i] = chrArr[i];
    matrix[n-1][n+i] = chrArr[i];
}

// print
for (int i = 0; i < n; i++) {
    for (int j = 0; j < 2*n+1; j++) {
        System.out.print(matrix[i][j]);
    }
    System.out.print("\n");
}

, ideone. .

0

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


All Articles