Skip to main content

Split the string into N equal parts

Here given code implementation process.

/*
    Java Program for
    Split the string into N equal parts
*/
public class Splitting
{
	// Returns the substring of given start and end position
	public String subString(String text, int start, int end)
	{
		String ans = "";
		for (int i = start; i < end; ++i)
		{
			ans += text.charAt(i);
		}
		return ans;
	}
	public void splitNequalString(String text, int n)
	{
		int size = text.length();
		if (size == 0 || n > size)
		{
			// Invalid partition
			return;
		}
		System.out.println("Given text : " + text);
		if ((size % n) != 0)
		{
			System.out.println("Given " + n + 
                               " equal partition are not possible");
		}
		else
		{
			// Use to collect partition result
			String[] result = new String[n];
			// Get starting growth of partition
			int growth = size / n;
			int start = 0;
			int end = growth;
			// Collect partition result
			for (int i = 0; i < n; ++i)
			{
				result[i] = subString(text, start, end);
				// Get next partition position
				start = end;
				end += growth;
			}
			System.out.println("Partition of " + n + " equal size");
			// Display partition result
			for (int i = 0; i < n; ++i)
			{
				System.out.println(result[i]);
			}
		}
	}
	public static void main(String[] args)
	{
		Splitting task = new Splitting();
		// Given string numbers
		String num1 = "1234568902";
		String num2 = "ABCDEFGHIKLMNOP";
		String num3 = "xyz";
		// Test Case
		task.splitNequalString(num1, 2);
		task.splitNequalString(num2, 5);
		task.splitNequalString(num3, 3);
	}
}

Output

Given text : 1234568902
Partition of 2 equal size
12345
68902
Given text : ABCDEFGHIKLMNOP
Partition of 5 equal size
ABC
DEF
GHI
KLM
NOP
Given text : xyz
Partition of 3 equal size
x
y
z
// Include header file
#include <iostream>
#include <string>

using namespace std;
/*
    C++ Program for
    Split the string into N equal parts
*/
class Splitting
{
	public:
		// Returns the substring of given start and end position
		string subString(string text, int start, int end)
		{
			string ans = "";
			for (int i = start; i < end; ++i)
			{
				ans += text[i];
			}
			return ans;
		}
	void splitNequalString(string text, int n)
	{
		int size = text.length();
		if (size == 0 || n > size)
		{
			// Invalid partition
			return;
		}
		cout << "Given text : " << text << endl;
		if ((size % n) != 0)
		{
			cout << "Given " << n 
          		 << " equal partition are not possible" << endl;
		}
		else
		{
			// Use to collect partition result
			string result[n];
			// Get starting growth of partition
			int growth = size / n;
			int start = 0;
			int end = growth;
			// Collect partition result
			for (int i = 0; i < n; ++i)
			{
				result[i] = this->subString(text, start, end);
				// Get next partition position
				start = end;
				end += growth;
			}
			cout << "Partition of " << n << " equal size" << endl;
			// Display partition result
			for (int i = 0; i < n; ++i)
			{
				cout << result[i] << endl;
			}
		}
	}
};
int main()
{
	Splitting *task = new Splitting();
	// Given string numbers
	string num1 = "1234568902";
	string num2 = "ABCDEFGHIKLMNOP";
	string num3 = "xyz";
	// Test Case
	task->splitNequalString(num1, 2);
	task->splitNequalString(num2, 5);
	task->splitNequalString(num3, 3);
	return 0;
}

Output

Given text : 1234568902
Partition of 2 equal size
12345
68902
Given text : ABCDEFGHIKLMNOP
Partition of 5 equal size
ABC
DEF
GHI
KLM
NOP
Given text : xyz
Partition of 3 equal size
x
y
z
// Include namespace system
using System;
/*
    Csharp Program for
    Split the string into N equal parts
*/
public class Splitting
{
	// Returns the substring of given start and end position
	public String subString(String text, int start, int end)
	{
		String ans = "";
		for (int i = start; i < end; ++i)
		{
			ans += text[i];
		}
		return ans;
	}
	public void splitNequalString(String text, int n)
	{
		int size = text.Length;
		if (size == 0 || n > size)
		{
			// Invalid partition
			return;
		}
		Console.WriteLine("Given text : " + text);
		if ((size % n) != 0)
		{
			Console.WriteLine("Given " + n + 
                              " equal partition are not possible");
		}
		else
		{
			// Use to collect partition result
			String[] result = new String[n];
			// Get starting growth of partition
			int growth = size / n;
			int start = 0;
			int end = growth;
			// Collect partition result
			for (int i = 0; i < n; ++i)
			{
				result[i] = this.subString(text, start, end);
				// Get next partition position
				start = end;
				end += growth;
			}
			Console.WriteLine("Partition of " + n + " equal size");
			// Display partition result
			for (int i = 0; i < n; ++i)
			{
				Console.WriteLine(result[i]);
			}
		}
	}
	public static void Main(String[] args)
	{
		Splitting task = new Splitting();
		// Given string numbers
		String num1 = "1234568902";
		String num2 = "ABCDEFGHIKLMNOP";
		String num3 = "xyz";
		// Test Case
		task.splitNequalString(num1, 2);
		task.splitNequalString(num2, 5);
		task.splitNequalString(num3, 3);
	}
}

Output

Given text : 1234568902
Partition of 2 equal size
12345
68902
Given text : ABCDEFGHIKLMNOP
Partition of 5 equal size
ABC
DEF
GHI
KLM
NOP
Given text : xyz
Partition of 3 equal size
x
y
z
package main

import "fmt"
/*
    Go Program for
    Split the string into N equal parts
*/

// Returns the substring of given start and end position
func subString(text string, start int, end int) string {
	var ans string = ""
	for i := start ; i < end ; i++ {
		ans += string(text[i])
	}
	return ans
}
func splitNequalString(text string, n int) {
	var size int = len(text)
	if size == 0 || n > size {
		// Invalid partition
		return
	}
	fmt.Println("Given text : ", text)
	if (size % n) != 0 {
		fmt.Println("Given ", n, " equal partition are not possible")
	} else {
		// Use to collect partition result
		var result = make([]string,n)
		// Get starting growth of partition
		var growth int = size / n
		var start int = 0
		var end int = growth
		// Collect partition result
		for i := 0 ; i < n ; i++ {
			result[i] = subString(text, start, end)
			// Get next partition position
			start = end
			end += growth
		}
		fmt.Println("Partition of ", n, " equal size")
		// Display partition result
		for i := 0 ; i < n ; i++ {
			fmt.Println(result[i])
		}
	}
}
func main() {
	
	// Given string numbers
	var num1 string = "1234568902"
	var num2 string = "ABCDEFGHIKLMNOP"
	var num3 string = "xyz"
	// Test Case
	splitNequalString(num1, 2)
	splitNequalString(num2, 5)
	splitNequalString(num3, 3)
}

Output

Given text :  1234568902
Partition of  2  equal size
12345
68902
Given text :  ABCDEFGHIKLMNOP
Partition of  5  equal size
ABC
DEF
GHI
KLM
NOP
Given text :  xyz
Partition of  3  equal size
x
y
z
<?php
/*
    Php Program for
    Split the string into N equal parts
*/
class Splitting
{
	// Returns the substring of given start and end position
	public	function subString($text, $start, $end)
	{
		$ans = "";
		for ($i = $start; $i < $end; ++$i)
		{
			$ans .= $text[$i];
		}
		return $ans;
	}
	public	function splitNequalString($text, $n)
	{
		$size = strlen($text);
		if ($size == 0 || $n > $size)
		{
			// Invalid partition
			return;
		}
		echo("Given text : ".$text."\n");
		if (($size % $n) != 0)
		{
			echo("Given ".$n.
				" equal partition are not possible\n");
		}
		else
		{
			// Use to collect partition result
			$result = array_fill(0, $n, "");
			// Get starting growth of partition
			$growth = (int)($size / $n);
			$start = 0;
			$end = $growth;
			// Collect partition result
			for ($i = 0; $i < $n; ++$i)
			{
				$result[$i] = $this->subString($text, $start, $end);
				// Get next partition position
				$start = $end;
				$end += $growth;
			}
			echo("Partition of ".$n." equal size\n");
			// Display partition result
			for ($i = 0; $i < $n; ++$i)
			{
				echo($result[$i]."\n");
			}
		}
	}
}

function main()
{
	$task = new Splitting();
	// Given string numbers
	$num1 = "1234568902";
	$num2 = "ABCDEFGHIKLMNOP";
	$num3 = "xyz";
	// Test Case
	$task->splitNequalString($num1, 2);
	$task->splitNequalString($num2, 5);
	$task->splitNequalString($num3, 3);
}
main();

Output

Given text : 1234568902
Partition of 2 equal size
12345
68902
Given text : ABCDEFGHIKLMNOP
Partition of 5 equal size
ABC
DEF
GHI
KLM
NOP
Given text : xyz
Partition of 3 equal size
x
y
z
/*
    Node JS Program for
    Split the string into N equal parts
*/
class Splitting
{
	// Returns the substring of given start and end position
	subString(text, start, end)
	{
		var ans = "";
		for (var i = start; i < end; ++i)
		{
			ans += text.charAt(i);
		}
		return ans;
	}
	splitNequalString(text, n)
	{
		var size = text.length;
		if (size == 0 || n > size)
		{
			// Invalid partition
			return;
		}
		console.log("Given text : " + text);
		if ((size % n) != 0)
		{
			console.log("Given " + n + " equal partition are not possible");
		}
		else
		{
			// Use to collect partition result
			var result = Array(n).fill(null);
			// Get starting growth of partition
			var growth = parseInt(size / n);
			var start = 0;
			var end = growth;
			// Collect partition result
			for (var i = 0; i < n; ++i)
			{
				result[i] = this.subString(text, start, end);
				// Get next partition position
				start = end;
				end += growth;
			}
			console.log("Partition of " + n + " equal size");
			// Display partition result
			for (var i = 0; i < n; ++i)
			{
				console.log(result[i]);
			}
		}
	}
}

function main()
{
	var task = new Splitting();
	// Given string numbers
	var num1 = "1234568902";
	var num2 = "ABCDEFGHIKLMNOP";
	var num3 = "xyz";
	// Test Case
	task.splitNequalString(num1, 2);
	task.splitNequalString(num2, 5);
	task.splitNequalString(num3, 3);
}
main();

Output

Given text : 1234568902
Partition of 2 equal size
12345
68902
Given text : ABCDEFGHIKLMNOP
Partition of 5 equal size
ABC
DEF
GHI
KLM
NOP
Given text : xyz
Partition of 3 equal size
x
y
z
#    Python 3 Program for
#    Split the string into N equal parts
class Splitting :
	#  Returns the substring of given start and end position
	def subString(self, text, start, ends) :
		ans = ""
		i = start
		while (i < ends) :
			ans += text[i]
			i += 1
		
		return ans
	
	def splitNequalString(self, text, n) :
		size = len(text)
		if (size == 0 or n > size) :
			#  Invalid partition
			return
		
		print("Given text : ", text)
		if ((size % n) != 0) :
			print("Given ", n ," equal partition are not possible")
		else :
			#  Use to collect partition result
			result = [None] * (n)
			#  Get starting growth of partition
			growth = int(size / n)
			start = 0
			ends = growth
			i = 0
			#  Collect partition result
			while (i < n) :
				result[i] = self.subString(text, start, ends)
				#  Get next partition position
				start = ends
				ends += growth
				i += 1
			
			print("Partition of ", n ," equal size")
			i = 0
			#  Display partition result
			while (i < n) :
				print(result[i])
				i += 1
			
		
	

def main() :
	task = Splitting()
	#  Given string numbers
	num1 = "1234568902"
	num2 = "ABCDEFGHIKLMNOP"
	num3 = "xyz"
	#  Test Case
	task.splitNequalString(num1, 2)
	task.splitNequalString(num2, 5)
	task.splitNequalString(num3, 3)

if __name__ == "__main__": main()

Output

Given text :  1234568902
Partition of  2  equal size
12345
68902
Given text :  ABCDEFGHIKLMNOP
Partition of  5  equal size
ABC
DEF
GHI
KLM
NOP
Given text :  xyz
Partition of  3  equal size
x
y
z
#    Ruby Program for
#    Split the string into N equal parts
class Splitting 
	#  Returns the substring of given start and end position
	def subString(text, start, ends) 
		ans = ""
		i = start
		while (i < ends) 
			ans += text[i]
			i += 1
		end

		return ans
	end

	def splitNequalString(text, n) 
		size = text.length
		if (size == 0 || n > size) 
			#  Invalid partition
			return
		end

		print("Given text : ", text, "\n")
		if ((size % n) != 0) 
			print("Given ", n ," equal partition are not possible", "\n")
		else
 
			#  Use to collect partition result
			result = Array.new(n) {""}
			#  Get starting growth of partition
			growth = size / n
			start = 0
			ends = growth
			i = 0
			#  Collect partition result
			while (i < n) 
				result[i] = self.subString(text, start, ends)
				#  Get next partition position
				start = ends
				ends += growth
				i += 1
			end

			print("Partition of ", n ," equal size", "\n")
			i = 0
			#  Display partition result
			while (i < n) 
				print(result[i], "\n")
				i += 1
			end

		end

	end

end

def main() 
	task = Splitting.new()
	#  Given string numbers
	num1 = "1234568902"
	num2 = "ABCDEFGHIKLMNOP"
	num3 = "xyz"
	#  Test Case
	task.splitNequalString(num1, 2)
	task.splitNequalString(num2, 5)
	task.splitNequalString(num3, 3)
end

main()

Output

Given text : 1234568902
Partition of 2 equal size
12345
68902
Given text : ABCDEFGHIKLMNOP
Partition of 5 equal size
ABC
DEF
GHI
KLM
NOP
Given text : xyz
Partition of 3 equal size
x
y
z
/*
    Scala Program for
    Split the string into N equal parts
*/
class Splitting()
{
	// Returns the substring of given start and end position
	def subString(text: String, start: Int, end: Int): String = {
		var ans: String = "";
		var i: Int = start;
		while (i < end)
		{
			ans += text.charAt(i);
			i += 1;
		}
		return ans;
	}
	def splitNequalString(text: String, n: Int): Unit = {
		var size: Int = text.length();
		if (size == 0 || n > size)
		{
			// Invalid partition
			return;
		}
		println("Given text : " + text);
		if ((size % n) != 0)
		{
			println("Given " + n + " equal partition are not possible");
		}
		else
		{
			// Use to collect partition result
			var result: Array[String] = Array.fill[String](n)("");
			// Get starting growth of partition
			var growth: Int = size / n;
			var start: Int = 0;
			var end: Int = growth;
			var i: Int = 0;
			// Collect partition result
			while (i < n)
			{
				result(i) = subString(text, start, end);
				// Get next partition position
				start = end;
				end += growth;
				i += 1;
			}
			println("Partition of " + n + " equal size");
			i = 0;
			// Display partition result
			while (i < n)
			{
				println(result(i));
				i += 1;
			}
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Splitting = new Splitting();
		// Given string numbers
		var num1: String = "1234568902";
		var num2: String = "ABCDEFGHIKLMNOP";
		var num3: String = "xyz";
		// Test Case
		task.splitNequalString(num1, 2);
		task.splitNequalString(num2, 5);
		task.splitNequalString(num3, 3);
	}
}

Output

Given text : 1234568902
Partition of 2 equal size
12345
68902
Given text : ABCDEFGHIKLMNOP
Partition of 5 equal size
ABC
DEF
GHI
KLM
NOP
Given text : xyz
Partition of 3 equal size
x
y
z
import Foundation;
/*
    Swift 4 Program for
    Split the string into N equal parts
*/
class Splitting
{
	// Returns the substring of given start and end position
	func subString(_ text: [Character], _ start: Int, _ end: Int) -> String
	{
		var ans: String = "";
		var i: Int = start;
		while (i < end)
		{
			ans += String(text[i]);
			i += 1;
		}
		return ans;
	}
	func splitNequalString(_ data: String, _ n: Int)
	{
      	let text = Array(data);
		let size: Int = text.count;
		if (size == 0 || n > size)
		{
			// Invalid partition
			return;
		}
		print("Given text : ", data);
		if ((size % n)  != 0)
		{
			print("Given ", n ," equal partition are not possible");
		}
		else
		{
			// Use to collect partition result
			var result: [String] = Array(repeating: "", count: n);
			// Get starting growth of partition
			let growth: Int = size / n;
			var start: Int = 0;
			var end: Int = growth;
			var i: Int = 0;
			// Collect partition result
			while (i < n)
			{
				result[i] = self.subString(text, start, end);
				// Get next partition position
				start = end;
				end += growth;
				i += 1;
			}
			print("Partition of ", n ," equal size");
			i = 0;
			// Display partition result
			while (i < n)
			{
				print(result[i]);
				i += 1;
			}
		}
	}
}
func main()
{
	let task: Splitting = Splitting();
	// Given string numbers
	let num1: String = "1234568902";
	let num2: String = "ABCDEFGHIKLMNOP";
	let num3: String = "xyz";
	// Test Case
	task.splitNequalString(num1, 2);
	task.splitNequalString(num2, 5);
	task.splitNequalString(num3, 3);
}
main();

Output

Given text :  1234568902
Partition of  2  equal size
12345
68902
Given text :  ABCDEFGHIKLMNOP
Partition of  5  equal size
ABC
DEF
GHI
KLM
NOP
Given text :  xyz
Partition of  3  equal size
x
y
z
/*
    Kotlin Program for
    Split the string into N equal parts
*/
class Splitting
{
	// Returns the substring of given start and end position
	fun subString(text: String, start: Int, end: Int): String 
	{
		var ans: String = "";
		var i: Int = start;
		while (i < end)
		{
			ans += text.get(i);
			i += 1;
		}
		return ans;
	}
	fun splitNequalString(text: String, n: Int): Unit
	{
		val size: Int = text.length;
		if (size == 0 || n > size)
		{
			// Invalid partition
			return;
		}
		println("Given text : " + text);
		if ((size % n) != 0)
		{
			println("Given " + n + " equal partition are not possible");
		}
		else
		{
			// Use to collect partition result
			val result: Array < String > = Array(n)
			{
				""
			};
			// Get starting growth of partition
			val growth: Int = size / n;
			var start: Int = 0;
			var end: Int = growth;
			var i: Int = 0;
			// Collect partition result
			while (i < n)
			{
				result[i] = this.subString(text, start, end);
				// Get next partition position
				start = end;
				end += growth;
				i += 1;
			}
			println("Partition of " + n + " equal size");
			i = 0;
			// Display partition result
			while (i < n)
			{
				println(result[i]);
				i += 1;
			}
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Splitting = Splitting();
	// Given string numbers
	val num1: String = "1234568902";
	val num2: String = "ABCDEFGHIKLMNOP";
	val num3: String = "xyz";
	// Test Case
	task.splitNequalString(num1, 2);
	task.splitNequalString(num2, 5);
	task.splitNequalString(num3, 3);
}

Output

Given text : 1234568902
Partition of 2 equal size
12345
68902
Given text : ABCDEFGHIKLMNOP
Partition of 5 equal size
ABC
DEF
GHI
KLM
NOP
Given text : xyz
Partition of 3 equal size
x
y
z




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment