Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Program to divide a string in 'N' equal parts

In this tutorial, we will learn how to divide a string into 'n' equal parts. But before moving further, if you are not familiar with the concept of string, then do check the article on Strings in Java.

Input: Enter the string: StudyTonight

Enter the value of n: 4

Output: On dividing the entered string into n equal parts, we have

Stu

dyT

oni

ght

Program 1: Divide a String into N equal parts

In this program, we will see how to divide a string into 'N' equal parts when the string is pre-defined.

Algorithm

  1. Start.
  2. Declare a string.
  3. Initialize it.
  4. Enter the value of n.
  5. Call a method to divide the string into 'N' equal parts.
  6. Check whether it is possible to divide the string into n equal parts.
  7. If not possible, then print the message invalid input, try again.
  8. Calculate the number of parts to find the division points.
  9. Use a for loop to print the divided strings
  10. Display the result.
  11. Stop.

Below is the code for the same in Java Language.

//Java Program to divide a string into n equal parts
public class Main  
{ 
    // Method to divide a string into n equal parts
    static void divide(String str, int n) 
    { 
    
        int sub_size; 
        // Check whether the string can be divided into n equal parts  
        if (str.length() % n != 0) 
        { 
            System.out.println("Invalid Input"); 
            System.out.println("String size is not divisible by n"); 
            System.out.println("Try Again"); 
            return; 
        } 
      
        // Calculate the number of parts to find the division points 
        sub_size = str.length() / n; 
          
        for (int i = 0; i< str.length(); i++) 
        { 
            if(i % sub_size == 0) 
            System.out.println();  
            System.out.print(str.charAt(i)); 
        } 
    } 
      
    // Driver Code 
    public static void main(String[] args) 
    { 
        
        String str = "abcdefghijklmnopqr"; 
        System.out.println("The entered string is: "+str);
        int n=6;
        System.out.println("On dividing the entered string into "+ n +" equal parts, we have ");
        // Print n equal parts of the string 
        divide(str, n); 
    } 
} 


The entered string is: abcdefghijklmnopqr
On dividing the entered string into 6 equal parts, we have

abc
def
ghi
jkl
mno
pqr

Program 2: Divide a String into N equal parts

In this program, we will see how to divide a string into 'n' equal parts when the string is user-defined. Here, firstly we will ask the user to enter the string and then divide the string into n equal parts.

Algorithm

  1. Start.
  2. Declare a string.
  3. Ask the user to initialize it.
  4. Declare a variable for 'N'.
  5. Ask the user to initialize the variable.
  6. Declare a temporary variable and initialize it to 0.
  7. Declare another variable to store the number of characters in each string.
  8. Declare a new array of string types to store the array of strings.
  9. Check whether it is possible to divide the string into n equal parts.
  10. If not possible, then print the message invalid input, try again.
  11. If possible, then use a for loop for the same.
  12. Iterate over all the characters present in the string.
  13. Divide the string into n equal parts using substring.
  14. Print all the divided strings.
  15. Stop.

Below is the code for the same in Java language.

//Java Program to divide the string into n equal parts
import java.util.*;

public class Main 
{  
    public static void main(String[] args) 
    {  
        //Take input from the user
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the string: ");
        String str = sc.nextLine();  
        //Enter the total number of parts 
        System.out.println("Enter the value of n: ");
        int n = sc.nextInt();  
        int temp = 0, chars = str.length()/n; 
        
        //Stores the array of string  
        String[] newStr = new String [n];  
        //Check whether a string can be divided into n equal parts  
        if(str.length() % n != 0) 
        {  
            System.out.println("Invalid Input"); 
            System.out.println("String size is not divisible by "+n); 
            System.out.println("Try Again"); 
        }  
        else 
        {  
            for(int i = 0; i < str.length() ; i = i+chars) 
            {  
                //Dividing string in n equal part using substring()  
                String part = str.substring(i, i+chars);  
                newStr[temp] = part;  
                temp++;  
            }  
               System.out.println("On dividing the entered string into "+ n +" equal parts, we have ");  
               for(int i = 0; i < newStr.length; i++) 
               {  
                   System.out.println(newStr[i]);  
               }  
            }  
        }  
}  


Enter the string: StudyTonight
Enter the value of n: 4
On dividing the entered string into 4 equal parts, we have
Stu
dyT
oni
ght