Javatpoint Logo
Javatpoint Logo

Program to print a string in vertical in Java

Description: You have a string and you need to print its characters vertically, starting from left to right of the string.

An array of characters can be referred to as a string. In this case, the program is built to print the letters vertically, beginning on the left side of the string and moving right. The string's characters are printed one per line. For instance, the string "ABC" has the three letters (or characters) "A," "B," and "C." This letter will now be printed in three distinct vertical lines as:

The small-letter characters will be changed to upper case, and the upper case characters should then be printed. To print the characters in this manner, each character is first accessible using its respective index position (the index starts from 0). The text will be printed in upper case if it is in lower case, and after each character is printed, the cursor should be moved to the next line so that the following character can be printed once more. The string's final character will be reached by repeating this technique.

Algorithm

Example

Output:

Program to print a string in vertical in Java

Time Complexity: This program has an O(n) time complexity, where n is the number of characters in the string.

Space Complexity: This program has an O(n) space complexity, where n is the string's character count.

Print a string in a vertical zigzag manner

The job is to print the supplied string in a vertical zigzag pattern with respect to the given number of rows when given a string, S, of size N and a number of rows, R, as shown in the examples.

Approach: The goal is to determine the distance between the major columns and a step value for the in-between columns in order to print the spaces until the string's final character is reached. To solve this issue, adhere to the methods listed below:

  • Determining a variable intervalas 2*R-2 to store the gap between the major columns.
  • Iterate in the range [0, R-1]using the variable i
    • Initialize a variable stepas interval-2*i to stock step values for each row.
    • Iterate in the range [i, N-1]using the variable j, incrementing j by interval in each iteration,
      • Print the character, S[j].
      • If the value of step sprawl in the range [1, interval-1]and step+j<N, then print (interval-R-i) number of spaces, then print s[j+step] and finally print (i-1)
      • Else print (interval-R)number of spaces.
    • Print New Line after each iteration of the outer loop.

Below is the implementation of the above approach:

Program code in Java

Output:

Program to print a string in vertical in Java

Time Complexity: O(R2*N)

Auxiliary Space: O(1)







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA