logo CodeStepByStep logo

zigzagConvert

Language/Type: Java string parameters return
Related Links:

Write a method named zigzagConvert that accepts a string and an integer representing a number of rows as parameters and returns a new string as follows. Think of the string's characters being displayed in columns into a "zigzag" pattern based on the given number of rows. For example, the call of zigzagConvert("ABCDEFGHIJKLMNOPQRS", 5) would think of the characters of the string as being placed into a pattern as follows:

         column
row 1 2 3 4 5 6 7 8 9 
 1  A       I       Q
 2  B     H J     P R
 3  C   G   K   O   S
 4  D F     L N
 5  E       M

Your function should reorder the letters to be read left-to-right in row-major order. Row 1 in our example contains "AIQ", then row 2 contains "BHJPR", and so on. So this call would return "AIQBHJPRCGKOSDFLNEM". If the number of rows is 0 or less, return an empty string.

Method: Write a Java method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.