Java Program to Divide a String in ‘N’ Equal Parts

Overview

In this blog, we will learn to write a Java program that divides a string in ‘N’ equal parts when the string is user-defined.

Program Description

Input a string from the user. The task is to divide the string into ‘N’ equal parts and print all the divided strings. The program must check if it is possible to divide the string into ‘N’ equal parts. If not, display an error message and exit the program.

Program Explanation

How It Works

Find the length of the string using the function “length()” (i.e. length = str.length()).

Check whether the string can be divided into ‘N’ equal parts. If the string can’t be divided into ‘N’ equal parts (i.e. length % N != 0), print a message and exit the program.

Else (i.e. length % N == 0), do the following:

  1. Declare a string array of size ‘N’ (say ‘dividedStr’).
  2. Calculate the size of each part by dividing the length of the string by ‘N’ (partSize = length / N);
  3. Loop through the input string. In the loop, extract a substring from the input string with incrementing character ‘i’ as starting index and ‘i + partSize’ as ending index. Assign the extracted substring to the array.
  4. Each time, the loop is incremented with a condition of ‘i += partSize’.
  5. During each loop execution, increment the array index.
  6. Now, the divided string parts are stored in the array which is printed on the screen.
Pseudo Code

Source Code

Here is the complete Java program to divide a given string into ‘N’ equal parts:

Source Code

Test Cases

1.Enter the string: GuviGeek

Enter the value of ‘N’: 2

2 equal parts of the given string are:

Guvi

Geek

2.Enter the string: GuviGeek

Enter the value of ‘N’: 3

GuviGeek cannot be divided into 3 equal parts!

Are you looking to master coding and crack top company interviews? Go ahead and try GUVI’s Pro Subscription to help you with everything needed to build a strong Technical Career.

Know more about GUVI:

Also, download GUVI’s app to learn the latest IT skills in vernacular languages.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Setup your CI/CD pipeline with Configuration as a Code and GitHub Actions in 1 minute.

12.1 Factor Apps: Dev/Prod Parity

12.1 Factor Apps: Dev/Prod Parity cover

DONE Understanding Of The “Definition Of DONE”

How to Enable User-Friendly URLs for Websites Through Apache HTTPD Server

A leap of faith into programming

Employee Spotlight: Chris Sean

REVERSED STRESS & DESIGN:FINITE & INFINITE LIFE

Downloading Extension Certificates from Azure DevOps Marketplace programmatically

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Baruni Priya T S

Baruni Priya T S

More from Medium

Java I/O Byte Stream Implementation

How to improve quality of the automated Java tests using Pitest

What is Eclipse MicroProfile?

jvm memory tuning