Printing string in rows and column pattern Java

前端 未结 7 2267
孤独总比滥情好
孤独总比滥情好 2021-02-07 14:31

i\'m just created a java project to print string that is given in rows and column just like matrix. Here\'s the output that i just made:

h e l l o 
_ w o r l 
d _         


        
7条回答
  •  悲哀的现实
    2021-02-07 15:24

    Basically, you move through the string from start to end, but treat the stringbuffer as an array. You#ll also need to to keep track of your direction (dx,dy) and where your bounds are.

    The following code will produce:

    hello
    beau 
     l.tw
    sufio
    i dlr
    

    given the input "hello world is beautiful"

    public class Main {
    
        public static void main(String[] args) {
            String text ="hello world is beautiful";
            int len = text.length();
            double sideLength = Math.sqrt( len );
            int width = 0;
            int height = 0;
    
            // check if it's a square
            if ( sideLength > (int) sideLength) {
                // nope... it#s a rectangle
                width = (int) sideLength +1;
                height = (int) Math.ceil((double)len / (double)width);
            } else {
                // square
                width = (int) sideLength;
                height = (int) sideLength;
            }
    
            // create a buffer for the spiral
            StringBuffer buf = new StringBuffer( width * height );
            buf.setLength( width * height );
            // clear it.
            for (int a=0; a < buf.length(); a++ ) {
                buf.setCharAt(a, '.');
            }
            
    
            int dstX = 0;
            int dstY = 0;
            int curWidth =  width;
            int curHeight = height;
            int startX = 0;
            int startY = 0;
            int dx = 1;
            int dy = 0;
            // go through the string, char by char
            for (int srcPos =0; srcPos < len; srcPos++) {
                buf.setCharAt( dstX + dstY * width, text.charAt( srcPos ));
    
                // move cursor
                dstX += dx;
                dstY += dy;
    
                // check for bounds
                if ( dstX == curWidth-1 && dx > 0) {
                    // end of line while going right, need to go down
                    dx = 0;
                    dy = 1;
                    // also, reduce width
                    curWidth--;
                    startY++;
                } else if (dstY == curHeight-1 && dy > 0) {
                    // end of column while going down, need to go left
                    dx = -1;
                    dy = 0;
    
                    // also, reduce height
                    curHeight--;
                } else if (dstX == startX && dx < 0) {
                    // hit left border while going left, need to go up
                    dx = 0;
                    dy = -1;
                    // also, increase startX
                    startX++;
                } else if (dstY == startY && dy < 0) {
                    // hit top border, while going up, need to go right
                    dx = 1;
                    dy = 0;
                    // also, increase startY
                    startY++;
                }
                
            }
    
    
            // display string
            for (int line = 0; line < height; line++) {
                System.out.println( buf.substring( line* width, line*width +width) );
            }
        }
    }
    
    

提交回复
热议问题