PUBLISHED ON: MARCH 11, 2021
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
- Start.
- Declare a string.
- Initialize it.
- Enter the value of n.
- Call a method to divide the string into 'N' equal parts.
- Check whether it is possible to divide the string into n equal parts.
- If not possible, then print the message invalid input, try again.
- Calculate the number of parts to find the division points.
- Use a for loop to print the divided strings
- Display the result.
- 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
- Start.
- Declare a string.
- Ask the user to initialize it.
- Declare a variable for 'N'.
- Ask the user to initialize the variable.
- Declare a temporary variable and initialize it to 0.
- Declare another variable to store the number of characters in each string.
- Declare a new array of string types to store the array of strings.
- Check whether it is possible to divide the string into n equal parts.
- If not possible, then print the message invalid input, try again.
- If possible, then use a for loop for the same.
- Iterate over all the characters present in the string.
- Divide the string into n equal parts using substring.
- Print all the divided strings.
- 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