简体   繁体   中英

Hollow Square of given String

I am trying to write a method that accepts 3 Paraments, and return a hollow square.

1st Method parameter = length of string. 2nd Method parameter = how many characters would be there in the one arm of the square. 3rd Method parameter = String.

Condition: Example if 2nd parameter is 4 so total 12 characters required to form a square, if given string is sorter the that then it will be repeated (abcdefghabcd).

I have written a code but that is not satisfying few conditions.

public void patternmaker(int a, int b, String s) {
    try {
    int p = -2, q = 0, z = 0;
    for (int x = 1; x <= b; x++) {
        for (int y = 1; y <= b; y++) {
            
                if (x == 1) {
                    System.out.print(s.charAt(y + b - 2) + " ");
                } else if (x == b && y > 1) {
                    System.out.print(s.charAt(a + z - 1) + " ");
                    z = z - 1;
                } else if (x == b && y == 1) {
                    System.out.print(s.charAt(0) + " ");

                } else if (y == 1 && x > 1 && x < b) {
                    System.out.print(s.charAt(b + p) + " ");
                    p = p - 1;
                } else if (y == b && x > 1 && x < b) {
                    System.out.print(s.charAt(2 * b - 2 + x - 1) + " ");
                } else {
                    System.out.print("  ");
                }
            } 
            System.out.println();

        }
    }
    catch (Exception e) {
    }

}

refer image for example

在此处输入图像描述

Any suggestion or input?

Here an answer that writes the string in a clockwise spiral:

    public void patternmaker(int a, int b, String s)
    {
        for(int y=b-1 ; y >=0 ; y--)
        {
            for(int x=0; x <b; x++)
            {
                if ( y == b-1)  // top row
                {
                    // left colum string indices 0,1,... such
                    // that x=0 is index b-1
                    System.out.print( s.charAt((b-1+x)%a) + " " ) ;
                }
                else if ( y < b-1 && y > 0) // intermediate rows
                {
                    if ( x==0 )
                        // left column
                        System.out.print( s.charAt(y%a) + " " ) ;
                    else if ( x== b-1 )
                        // right column top element is index 2*b-2
                        System.out.print( s.charAt((2*b-2+(b-1-y))%a) + " " ) ;
                    else
                        // hole
                        System.out.print( "  " ) ;
                }
                else // bottom row
                {
                    if ( x > 0 )
                        // rightmost elemnt is index 3*b-3, increase by 1 if x decreases by 1
                        System.out.print( s.charAt((3*b-3+(b-1-x)) %a) + " " ) ;
                    else
                        System.out.print( s.charAt(0) + " " ) ;
                }
            }
            System.out.println() ;
        }
    }

There are multiple ways to approach this problem.

Method 1

Create a 2-D array with dimensions same as side of the square. Iterate over the string elements and populate the array. Print all elements of the array.
This, although easy to implement, will require a lot of memory as the square grows larger.

Method 2

This method is more efficient (both time and memory) compared to the first.

public class Main
{
    public static void patternmaker(int length, int side, String s) {
        try {
            char ch;
            int idx = side - 1;
            //first row
            for (; idx <= 2*(side - 1) ; idx++)
            {
                ch = s.charAt(idx%length);
                System.out.print(ch + " ");
            }
            System.out.println();
            //middle rows
            for (; idx < 3*(side - 1) ; idx++)
            {
                ch = s.charAt((3*(side - 1) - idx)%length);
                System.out.print(ch + " ");
                for (int spaceNo = 2 ; spaceNo < side ; spaceNo++)
                {
                    System.out.print("  ");//Two spaces inside
                }
                ch = s.charAt(idx%length);
                System.out.println(ch + " ");
            }
            //last row
            System.out.print(s.charAt(0) + " ");
            for (idx = 4*(side - 1) - 1 ; idx >= 3*(side -1); idx--)
            {
                ch = s.charAt(idx%length);
                System.out.print(ch + " ");
            }
            System.out.println();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        
    }
    
    public static void main(String[] args) {
        patternmaker(8, 6, "12345678");
        System.out.println();
        patternmaker(3, 4, "123");
        System.out.println();
        patternmaker(2, 5, "12");
        System.out.println();
        patternmaker(5, 5, "13579");
        System.out.println();
        patternmaker(4, 8, "2468");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM