How to print string vertically in Java
To print a string vertically in Java, you have to put each character on a separate line. There are several ways to accomplish this task, including using recursion, loops, and string manipulation strategies.
Example 1:
Input: String = “Hello”
Output:
H
e
l
l
o
Example 2:
Input: String = “Java”
Output:
J
a
v
a
Approach: Naive
A basic loop would be used to iterate through each character in the string and print it one at a time, on a new line, in the naive method of printing a string vertically.
Here’s the implementation of the naive approach :
FileName: VerticalStringPrinter.java
public class VerticalStringPrinter {
// Method to print the string vertically
public static void printVertically(String text) {
// Loop through each character of the string
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i); // Get the current character
System.out.println(c);
}
}
public static void main(String args[])
{
String text = " TutorialAndExample";
printVertically(text);
}
}
Output:
T
u
t
o
r
i
a
l
A
n
d
E
x
a
m
p
l
e
Complexity analysis: The naive vertical printing method has a linear time complexity of O(n) and a constant space complexity of O(1) due to its fixed memory usage.
Optimized Approach
Reducing pointless operations or enhancing performance could be two aspects of an optimized vertical string printing strategy.
Here's an optimized approach using a `StringBuilder` :
FineName:VerticalStringPrinter1.java
public class VerticalStringPrinter1 {
// Method to print the string vertically
public static void printVertically(String text) {
// Create a StringBuilder to construct the vertical string
StringBuilder verticalText = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
verticalText.append(c).append('\n'); // Append the character followed by a newline
}
System.out.print(verticalText);
}
public static void main(String args[])
{
String text = "TutorialAndExample";
printVertically(text); }
}
Output:
T
u
t
o
r
i
a
l
A
n
d
E
x
a
m
p
l
e
Complexity analysis: The optimized vertical printing approach has a time complexity of O(n) and a space complexity of O(n) due to linear time and space usage.
Approach: Using Recursion
A recursive solution for vertically printing a string involves defining a function that iterates over each character and prints them, then calling itself recursively until all characters are printed.
Here’s the implementation of the recursive approach:
FileName:VerticalStringPrinter2.java
public class VerticalStringPrinter {
public static void printVertically(String text) {
printVertically(text, 0); // Call the helper function to start recursion
}
// Helper function to print characters vertically starting from the given index
private static void printVertically(String text, int index) {
if (index < text.length()) {
char c = text.charAt(index);
System.out.println(c);
printVertically(text, index + 1); // Recur with the next index
}
}
public static void main(String args[]) {
String text = " TutorialAndExample";
printVertically(text);
}
}
Output:
T
u
t
o
r
i
a
l
A
n
d
E
x
a
m
p
l
e
Complexity analysis: The recursive approach for vertical string printing has a time complexity of O(n), with each call processing one character and a space complexity of O(n) due to the recursion stack.
Approach: Using Streams
Using streams to print a string vertically in Java requires taking advantage of functional programming features that were added in Java 8 and later versions. By first turning the string into a stream of characters and then processing each character separately, this method provides a concise and sophisticated solution.
Here’s the implementation of printing a string vertically in Java:
FileName:VerticalStringPrinter3.java
import java.util.stream.IntStream;
public class VerticalStringPrinter {
// Method to print the string vertically using streams
public static void printVertically(String text)
{
text.chars()
// Convert the string to an IntStream of Unicode code points
.mapToObj(c -> (char) c)
// Convert each code point to a character
.forEach(System.out::println);
}
public static void main(String args[])
{
String text = "TutorialAndExample";
System.out.println(text);
System.out.println("\nvertically printed string using streams:");
printVertically(text);
}
}
Output:
TutorialAndExample
vertically printed string using streams:
T
u
t
o
r
i
a
l
A
n
d
E
x
a
m
p
l
e
Complexity analysis: Time Complexity is O(n), where n is the length of the input string, to print a string vertically in Java using streams. Stream operations like `chars()` and `forEach()` perform a single iteration through every character. Space Complexity is O(1), constant because the amount of space needed is independent of the length of the input string.