In this tutorial, we will go through the string pattern program in java. Before that, you may go through the following topic in java.
Character Pattern Programs in Java
The program below takes a user input for a word from the user using Scanner class and prints that word in various patterns format in java. The following are the three alphabet patterns in java with string input.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import java.util.Scanner; public class Main { public static void main(String args[]) { int i, j; String str; char[] ch; Scanner sc = new Scanner(System.in); System.out.print("Enter the string: "); str = sc.nextLine(); ch = str.toCharArray(); //upper part for (i = 0; i < ch.length; i++) { for (j = 0; j < i; j++) System.out.print(ch[j] + " "); System.out.println(); } //lower part for (i = ch.length; i >= 0; i--) { for (j = 0; j < i; j++) System.out.print(ch[j] + " "); System.out.println(); } } } |
Output:
Enter the string: SIMPLE
S
S I
S I M
S I M P
S I M P L
S I M P L E
S I M P L
S I M P
S I M
S I
S