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


In this article, we will understand how to divide a string in 'N' equal parts. String is a datatype that contains one or more characters and is enclosed in double quotes(“ ”).

Below is a demonstration of the same −

Suppose our input is

Input string: Java Program is fun!

The desired output would be

The length of the string is: 20
4 equal parts of given string are
Java
Progr
am is
fun!

Algorithm

Step 1 - START
Step 2 - Declare a string namely input_string, two integers namely string_length and N.
Step 3 - Define the values.
Step 4 - Initiatize a temporary variable to 0.
Step 5 - Compute the parts in the string by dividing the length of the string by ‘N’.
Step 6 - If the string is not divisible by N, display a relevant message. Given that the string length is divisible by N, iterate through the string.
Step 7 - Fetch the substring within the range of string length and N in every iteration. Assign this value to a variable.
Step 8 - Increment the temporary variable after every iteration.
Step 9 - Display the N parts of the string on the console by iterating over the split parts of the string.
Step 10 - Stop

Example 1

Here, we bind all the operations together under the ‘main’ function.

public class Demo {
   public static void main(String[] args) {
      String input_string = "Java Program is fun!";
      System.out.println("The string is defined as: " +input_string);
      int string_length = input_string.length();
      System.out.println("The length of the string is: " +string_length);
      int N = 4;
      int temp = 0, string_parts = string_length/N;
      String[] equalStr = new String [N];
      if(string_length % N != 0) {
      System.out.println("The string cannot be divided int "+ N +" parts.");
      } else {
         for(int i = 0; i < string_length; i = i+string_parts) {
            String part = input_string.substring(i, i+string_parts);
            equalStr[temp] = part;
            temp++;
         }
         System.out.println(N + " equal parts of given string are ");
         for(int i = 0; i < equalStr.length; i++) {
            System.out.println(equalStr[i]);
         }
      }
   }
}

Output

The string is defined as: Java Program is fun!
The length of the string is: 20
4 equal parts of given string are
Java
Progr
am is
fun!

Example 2

Here, we encapsulate the operations into functions exhibiting object-oriented programming.

public class Demo {
   static void divide_string(String input_string, int N){
      int string_length = input_string.length();
      System.out.println("The length of the string is: " +string_length);
      int temp = 0, string_parts = string_length/N;
      String[] equalStr = new String [N];
      if(string_length % N != 0) {
      System.out.println("The string cannot be divided int "+ N +" parts.");
      } else {
         for(int i = 0; i < string_length; i = i+string_parts) {
            String part = input_string.substring(i, i+string_parts);
            equalStr[temp] = part;
            temp++;
         }
         System.out.println(N + " equal parts of given string are ");
         for(int i = 0; i < equalStr.length; i++) {
            System.out.println(equalStr[i]);
         }
      }
   }
   public static void main(String[] args) {
      String input_string = "Java Program is fun!";
      System.out.println("The string is defined as: " +input_string);
      int N = 4;
      divide_string(input_string, N);
   }
}

Output

The string is defined as: Java Program is fun!
The length of the string is: 20
4 equal parts of given string are
Java
Progr
am is
fun!

Updated on: 30-Mar-2022

443 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements