String Pattern Programs in Java

In this post, we will develop many String Pattern Programs in Java. For example:- Half pyramid string pattern, Half diamond string pattern, Mirrored half diamond string pattern, and e.t.c.

String Pattern Programs in Java – 1 | Write a Java program to print the given below pattern. Solve this problem using a nested loop without an array.

Enter a word: PHYSICS
   P
   H
   Y
PHYSICS
   I
   C
   S

In this pattern, the number of rows is equal to the size of the string. Therefore the outer loop will iterate from 0 to size, assume the “size” variable holds the length of the string.

When row-number is equal to size/2 then the pattern contains the complete word. In other cases, it contains size/2 spaces and then a character => word.carAt(row-number-1). For example:- in 1st row, it contains 3 spaces and ‘P’ i.e. word.charAt(0).

The Java program for the above String pattern,

import java.util.Scanner;
public class Test {
   public static void main(String[] args) {

      Scanner scan = new Scanner(System.in);
      System.out.print("Enter a word: ");
      String word = scan.nextLine();
      int size = word.length();

      for (int i=0; i<size; i++) {
        for (int j=0; j<=size/2; j++) {
          if(i == size/2 ){
            System.out.print(word);
            break;
          } else {
            if(j == size/2)
              System.out.print(word.charAt(i));
            else
              System.out.print(" "); // one space
          }
        }
        System.out.println(); // new line
      }
   }
}

Sample output of the above program:-

Enter a word: PROGRAM
   P
   R
   O
PROGRAM
   R
   A
   M

Half Pyramid String Pattern Programs in Java

String Pattern Programs in Java – 2 | Write a Java program to print the given below half pyramid string pattern in Java.

Enter a word: PROGRAM
P
PR
PRO
PROG
PROGR
PROGRA
PROGRAM

PrepBytes
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses

Java program for the above pattern,

This pattern contains N characters in the Nth line i.e. in the 1st line it contains 1 character, the 2nd line contains 2 characters, and so on. We can assume it as a matrix representation where lines will represent by rows and characters will be represented by columns. Therefore 1st row contains 1 column, the 2nd row contains 2 columns, and so on.

The inner loop (column) is dependent upon the row number. In 1st row, there should be only 1 column and only 1 character should be displayed. Therefore iterate the inner loop until the current row value. After execution of each inner loop print in the new line.

import java.util.Scanner;
public class Test {
   public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter a word: ");
      String word = scan.nextLine();

      for (int i=0; i<word.length(); i++) {
        for (int j=0; j<=i; j++) {
          System.out.print(word.charAt(j));
        }
        System.out.println(); // new line
      }
   }
}

Half Diamond String Pattern Programs in Java

String Pattern Programs in Java – 3 | Write a Java program to print the given below half diamond string pattern in Java.

Enter a word: PROGRAM
P
PR
PRO
PROG
PROGR
PROGRA
PROGRAM
PROGRA
PROGR
PROG
PRO
PR
P

Java program for the above half diamond string pattern,

import java.util.Scanner;
public class Test {
   public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter a word: ");
      String word = scan.nextLine();
      int size = word.length();

      for (int i=0; i < (2*size-1); i++) {
        int column = (i<size) ? i : (2*size-i-2);
        for (int j=0; j<=column; j++) {
          System.out.print(word.charAt(j));
        }
        System.out.println(); // new line
      }
   }
}

String Pattern Programs in Java – 4 | Write a Java program to print the given below pattern.

Enter a word: PROGRAM
P
PR
PRO
PROG
PROGR
PROGRA
PROGRAM
ARGORP
RGORP
GORP
ORP
RP
P

Java program for the above half diamond string pattern,

import java.util.Scanner;
public class Test {
   public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter a word: ");
      String word = scan.nextLine();
      int size = word.length();

      for (int i=0; i < (2*size-1); i++) {
        if(i<size){
          for (int j=0; j<=i; j++) {
            System.out.print(word.charAt(j));
          }
        } else{
          for (int j=(2*size-i-2); j>=0; j--) {
            System.out.print(word.charAt(j));
          }
        }
        System.out.println(); // new line
      }
   }
}

String Pattern Programs in Java – 5 | Write a Java program to print the given below half diamond string pattern in Java.

Enter a word: PROGRAM
P
PR
PRO
PROG
PROGR
PROGRA
PROGRAM
ROGRAM
OGRAM
GRAM
RAM
AM
M

Java program for the above half diamond string pattern,

import java.util.Scanner;
public class Test {
   public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter a word: ");
      String word = scan.nextLine();
      int size = word.length();

      for (int i=0; i < (2*size-1); i++) {
        if(i < size){
          for (int j=0; j<=i; j++) {
            System.out.print(word.charAt(j));
          }
        } else{
          for (int j=(i-size+1); j<size; j++) {
            System.out.print(word.charAt(j));
          }
        }
        System.out.println(); // new line
      }
   }
}

Mirrored Half Diamond String Pattern in Java

String Pattern Programs in Java – 6 | Write a Java program to print the given below mirrored half diamond string pattern in Java.

Enter a word: PROGRAM
      P
     PR
    PRO
   PROG
  PROGR
 PROGRA
PROGRAM
 ROGRAM
  OGRAM
   GRAM
    RAM
     AM
      M

Java program for the above mirrored half diamond string pattern,

import java.util.Scanner;
public class Test {
   public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter a word: ");
      String word = scan.nextLine();
      int len = word.length();

      // for first half
      for(int i=0; i<len-1; i++) {
        for(int j=0; j<len-i-1; j++) {
          System.out.print(" "); // space
        }
        for(int j=0; j<=i; j++) {
          System.out.print(word.charAt(j)); // character
        }
        System.out.println(); // new line
      }

      // for second half
      for(int i=0; i<len; i++) {
        for(int j=0; j<i; j++) {
          System.out.print(" "); // space
        }
        for(int j=i; j<len; j++) {
          System.out.print(word.charAt(j)); // character
        }
        System.out.println(); // new line
      }
   }
}

Pyramid String Pattern Programs in Java

String Pattern Programs in Java – 7 | Write a Java program to print the given below pyramid string pattern in Java.

Enter a word: PROGRAM
Enter number of lines: 6
       P 
      R O 
     G R A 
    M P R O 
   G R A M P 
  R O G R A M 
 P R O G R A M 

Java program for the above pyramid string pattern,

import java.util.Scanner;
public class Test {
   public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter a word: ");
      String word = scan.nextLine();
      System.out.print("Enter number of lines: ");
      int n = scan.nextInt();
      int len = word.length();
      int a = 0;

      for(int i=0; i<=n; i++) {
        // for space
        for (int j=0; j<=n-i; j++) {
           System.out.print(" "); // print space
        }
        for(int k=0; k<=i; k++) {
           // print character
           System.out.print(word.charAt(a)+" ");
           a++;

           // if index reach end of string then again
           // it should start from initial characters
           if(a == len) a = 0;
        }
        System.out.println(); // new line
      }
   }
}

In this program number of rows and strings is taken from the user. The number of columns in each is equal to the row number. So first we give space where space is (total row – current row). We have taken one separate variable “a” to keep track of the index of string. In the pattern when the string reached the end, then displaying again starts from the start of the string. For example in the word “PROGRAM”, when the pattern reached character “M” then again it starts from character “A”.

Similar pattern programs in Java,

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Mern Stack Web Development
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses

Leave a Comment

Your email address will not be published. Required fields are marked *