Program to print a string in vertical zigzag manner | Function to print any string in zigzag fashion ; Store the gap between the major columns ; Traverse through rows ; Store the step value for each row ; Iterate in the range [ 1 , N - 1 ] ; Print the character ; Print the spaces before character s [ j + step ] ; Print the character ; Print the spaces after character after s [ j + step ] ; Print the spaces for first and last rows ; Driver Code ; Given Input ; Function Call
def zigzag ( s , rows ) : NEW_LINE INDENT interval = 2 * rows - 2 NEW_LINE for i in range ( rows ) : NEW_LINE INDENT step = interval - 2 * i NEW_LINE for j in range ( i , len ( s ) , interval ) : NEW_LINE INDENT print ( s [ j ] , end = " " ) NEW_LINE if ( step > 0 and step < interval and step + j < len ( s ) ) : NEW_LINE INDENT for k in range ( ( interval - rows - i ) ) : NEW_LINE INDENT print ( end = " ▁ " ) NEW_LINE DEDENT print ( s [ j + step ] , end = " " ) NEW_LINE for k in range ( i - 1 ) : NEW_LINE INDENT print ( end = " ▁ " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT for k in range ( interval - rows ) : NEW_LINE INDENT print ( end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "123456789ABCDEFGHIJKL " " MNOPQRSTUVWXYZabcdefghi " rows = 9 NEW_LINE zigzag ( s , rows ) NEW_LINE DEDENT