Divide a string into n equal parts

Here given code implementation process.

/*
    C program for
    Divide a string into n equal parts
*/
#include <stdio.h>
#include <string.h>

void nEqualPartition(char *text, int n)
{
	// Get the length of given text
	int length = strlen(text);
	// Display Given text
	printf("\n Given text : %s ", text);
	// Display partition size
	printf("\n Partition size : %d \n ", n);
	if (length == 0 || n <= 0 || (length % n) != 0)
	{
		// When of partition not possible
		printf("\n Equal partition not possible of size %d", n);
	}
	else
	{
		// Get length of each part
		int part = length / n;
		// Display result 
		for (int i = 0; i < length; ++i)
		{
			printf("%c", text[i]);
			if ((i + 1) % part == 0)
			{
				// Separate the result by new line
				printf("\n ");
			}
		}
	}
}
int main(int argc, char
	const *argv[])
{
	char *text = "abcdefghijklmnopqrst";
	// partition n = 4
	nEqualPartition(text, 4);
	// partition n = 5
	nEqualPartition(text, 5);
	// partition n = 3
	nEqualPartition(text, 3);
	return 0;
}

Output

 Given text : abcdefghijklmnopqrst
 Partition size : 4
 abcde
 fghij
 klmno
 pqrst

 Given text : abcdefghijklmnopqrst
 Partition size : 5
 abcd
 efgh
 ijkl
 mnop
 qrst

 Given text : abcdefghijklmnopqrst
 Partition size : 3

 Equal partition not possible of size 3
// Java program for
// Divide a string into n equal parts
public class Partition
{
	public void nEqualPartition(String text, int n)
	{
		// Get the length of given text
		int length = text.length();
		// Display Given text
		System.out.print("\n Given text : " + text);
		// Display partition size
		System.out.print("\n Partition size : " + n + " \n ");
		if (length == 0 || n <= 0 || (length % n) != 0)
		{
			// When of partition not possible
			System.out.print("\n Equal partition not possible of size " + n);
		}
		else
		{
			// Get length of each part
			int part = length / n;
			// Display result 
			for (int i = 0; i < length; ++i)
			{
				System.out.print(text.charAt(i));
				if ((i + 1) % part == 0)
				{
					// Separate the result by new line
					System.out.print("\n ");
				}
			}
		}
	}
	public static void main(String[] args)
	{
		Partition task = new Partition();
		String text = "abcdefghijklmnopqrst";
		// partition n = 4
		task.nEqualPartition(text, 4);
		// partition n = 5
		task.nEqualPartition(text, 5);
		// partition n = 3
		task.nEqualPartition(text, 3);
	}
}

Output

 Given text : abcdefghijklmnopqrst
 Partition size : 4
 abcde
 fghij
 klmno
 pqrst

 Given text : abcdefghijklmnopqrst
 Partition size : 5
 abcd
 efgh
 ijkl
 mnop
 qrst

 Given text : abcdefghijklmnopqrst
 Partition size : 3

 Equal partition not possible of size 3
// Include header file
#include <iostream>
#include <string>

using namespace std;
// C++ program for
// Divide a string into n equal parts
class Partition
{
	public: void nEqualPartition(string text, int n)
	{
		// Get the length of given text
		int length = text.length();
		// Display Given text
		cout << "\n Given text : " << text;
		// Display partition size
		cout << "\n Partition size : " << n << " \n ";
		if (length == 0 || n <= 0 || (length % n) != 0)
		{
			// When of partition not possible
			cout << "\n Equal partition not possible of size " << n;
		}
		else
		{
			// Get length of each part
			int part = length / n;
			// Display result
			for (int i = 0; i < length; ++i)
			{
				cout << text[i];
				if ((i + 1) % part == 0)
				{
					// Separate the result by new line
					cout << "\n ";
				}
			}
		}
	}
};
int main()
{
	Partition *task = new Partition();
	string text = "abcdefghijklmnopqrst";
	// partition n = 4
	task->nEqualPartition(text, 4);
	// partition n = 5
	task->nEqualPartition(text, 5);
	// partition n = 3
	task->nEqualPartition(text, 3);
	return 0;
}

Output

 Given text : abcdefghijklmnopqrst
 Partition size : 4
 abcde
 fghij
 klmno
 pqrst

 Given text : abcdefghijklmnopqrst
 Partition size : 5
 abcd
 efgh
 ijkl
 mnop
 qrst

 Given text : abcdefghijklmnopqrst
 Partition size : 3

 Equal partition not possible of size 3
// Include namespace system
using System;
// Csharp program for
// Divide a string into n equal parts
public class Partition
{
	public void nEqualPartition(String text, int n)
	{
		// Get the length of given text
		int length = text.Length;
		// Display Given text
		Console.Write("\n Given text : " + text);
		// Display partition size
		Console.Write("\n Partition size : " + n + " \n ");
		if (length == 0 || n <= 0 || (length % n) != 0)
		{
			// When of partition not possible
			Console.Write("\n Equal partition not possible of size " + n);
		}
		else
		{
			// Get length of each part
			int part = length / n;
			// Display result
			for (int i = 0; i < length; ++i)
			{
				Console.Write(text[i]);
				if ((i + 1) % part == 0)
				{
					// Separate the result by new line
					Console.Write("\n ");
				}
			}
		}
	}
	public static void Main(String[] args)
	{
		Partition task = new Partition();
		String text = "abcdefghijklmnopqrst";
		// partition n = 4
		task.nEqualPartition(text, 4);
		// partition n = 5
		task.nEqualPartition(text, 5);
		// partition n = 3
		task.nEqualPartition(text, 3);
	}
}

Output

 Given text : abcdefghijklmnopqrst
 Partition size : 4
 abcde
 fghij
 klmno
 pqrst

 Given text : abcdefghijklmnopqrst
 Partition size : 5
 abcd
 efgh
 ijkl
 mnop
 qrst

 Given text : abcdefghijklmnopqrst
 Partition size : 3

 Equal partition not possible of size 3
package main
import "fmt"
// Go program for
// Divide a string into n equal parts

func nEqualPartition(text string, n int) {
	// Get the length of given text
	var length int = len(text)
	// Display Given text
	fmt.Print("\n Given text : ", text)
	// Display partition size
	fmt.Print("\n Partition size : ", n, " \n ")
	if length == 0 || n <= 0 || (length % n) != 0 {
		// When of partition not possible
		fmt.Print("\n Equal partition not possible of size ", n)
	} else {
		// Get length of each part
		var part int = length / n
		// Display result
		for i := 0 ; i < length ; i++ {
			fmt.Print(string(text[i]))
			if (i + 1) % part == 0 {
				// Separate the result by new line
				fmt.Print("\n ")
			}
		}
	}
}
func main() {

	var text string = "abcdefghijklmnopqrst"
	// partition n = 4
	nEqualPartition(text, 4)
	// partition n = 5
	nEqualPartition(text, 5)
	// partition n = 3
	nEqualPartition(text, 3)
}

Output

 Given text : abcdefghijklmnopqrst
 Partition size : 4
 abcde
 fghij
 klmno
 pqrst

 Given text : abcdefghijklmnopqrst
 Partition size : 5
 abcd
 efgh
 ijkl
 mnop
 qrst

 Given text : abcdefghijklmnopqrst
 Partition size : 3

 Equal partition not possible of size 3
<?php
// Php program for
// Divide a string into n equal parts
class Partition
{
	public	function nEqualPartition($text, $n)
	{
		// Get the length of given text
		$length = strlen($text);
		// Display Given text
		echo("\n Given text : ".$text);
		// Display partition size
		echo("\n Partition size : ".$n.
			" \n ");
		if ($length == 0 || $n <= 0 || ($length % $n) != 0)
		{
			// When of partition not possible
			echo("\n Equal partition not possible of size ".$n);
		}
		else
		{
			// Get length of each part
			$part = (int)($length / $n);
			// Display result
			for ($i = 0; $i < $length; ++$i)
			{
				echo($text[$i]);
				if (($i + 1) % $part == 0)
				{
					// Separate the result by new line
					echo("\n ");
				}
			}
		}
	}
}

function main()
{
	$task = new Partition();
	$text = "abcdefghijklmnopqrst";
	// partition n = 4
	$task->nEqualPartition($text, 4);
	// partition n = 5
	$task->nEqualPartition($text, 5);
	// partition n = 3
	$task->nEqualPartition($text, 3);
}
main();

Output

 Given text : abcdefghijklmnopqrst
 Partition size : 4
 abcde
 fghij
 klmno
 pqrst

 Given text : abcdefghijklmnopqrst
 Partition size : 5
 abcd
 efgh
 ijkl
 mnop
 qrst

 Given text : abcdefghijklmnopqrst
 Partition size : 3

 Equal partition not possible of size 3
// Node JS program for
// Divide a string into n equal parts
class Partition
{
	nEqualPartition(text, n)
	{
		// Get the length of given text
		var length = text.length;
		// Display Given text
		process.stdout.write("\n Given text : " + text);
		// Display partition size
		process.stdout.write("\n Partition size : " + n + " \n ");
		if (length == 0 || n <= 0 || (length % n) != 0)
		{
			// When of partition not possible
			process.stdout.write("\n Equal partition not possible of size " + n);
		}
		else
		{
			// Get length of each part
			var part = parseInt(length / n);
			// Display result
			for (var i = 0; i < length; ++i)
			{
				process.stdout.write(text.charAt(i));
				if ((i + 1) % part == 0)
				{
					// Separate the result by new line
					process.stdout.write("\n ");
				}
			}
		}
	}
}

function main()
{
	var task = new Partition();
	var text = "abcdefghijklmnopqrst";
	// partition n = 4
	task.nEqualPartition(text, 4);
	// partition n = 5
	task.nEqualPartition(text, 5);
	// partition n = 3
	task.nEqualPartition(text, 3);
}
main();

Output

 Given text : abcdefghijklmnopqrst
 Partition size : 4
 abcde
 fghij
 klmno
 pqrst

 Given text : abcdefghijklmnopqrst
 Partition size : 5
 abcd
 efgh
 ijkl
 mnop
 qrst

 Given text : abcdefghijklmnopqrst
 Partition size : 3

 Equal partition not possible of size 3
#  Python 3 program for
#  Divide a string into n equal parts
class Partition :
	def nEqualPartition(self, text, n) :
		#  Get the length of given text
		length = len(text)
		#  Display Given text
		print("\n Given text : ", text, end = "")
		#  Display partition size
		print("\n Partition size : ", n ," \n ", end = "")
		if (length == 0 or n <= 0 or(length % n) != 0) :
			#  When of partition not possible
			print("\n Equal partition not possible of size ", n, end = "")
		else :
			#  Get length of each part
			part = int(length / n)
			i = 0
			#  Display result
			while (i < length) :
				print(text[i], end = "")
				if ((i + 1) % part == 0) :
					#  Separate the result by new line
					print("\n ", end = "")
				
				i += 1
			
		
	

def main() :
	task = Partition()
	text = "abcdefghijklmnopqrst"
	#  partition n = 4
	task.nEqualPartition(text, 4)
	#  partition n = 5
	task.nEqualPartition(text, 5)
	#  partition n = 3
	task.nEqualPartition(text, 3)

if __name__ == "__main__": main()

Output

 Given text :  abcdefghijklmnopqrst
 Partition size :  4
 abcde
 fghij
 klmno
 pqrst

 Given text :  abcdefghijklmnopqrst
 Partition size :  5
 abcd
 efgh
 ijkl
 mnop
 qrst

 Given text :  abcdefghijklmnopqrst
 Partition size :  3

 Equal partition not possible of size  3
#  Ruby program for
#  Divide a string into n equal parts
class Partition 
	def nEqualPartition(text, n) 
		#  Get the length of given text
		length = text.length
		#  Display Given text
		print("\n Given text : ", text)
		#  Display partition size
		print("\n Partition size : ", n ," \n ")
		if (length == 0 || n <= 0 || (length % n) != 0) 
			#  When of partition not possible
			print("\n Equal partition not possible of size ", n)
		else
 
			#  Get length of each part
			part = length / n
			i = 0
			#  Display result
			while (i < length) 
				print(text[i])
				if ((i + 1) % part == 0) 
					#  Separate the result by new line
					print("\n ")
				end

				i += 1
			end

		end

	end

end

def main() 
	task = Partition.new()
	text = "abcdefghijklmnopqrst"
	#  partition n = 4
	task.nEqualPartition(text, 4)
	#  partition n = 5
	task.nEqualPartition(text, 5)
	#  partition n = 3
	task.nEqualPartition(text, 3)
end

main()

Output

 Given text : abcdefghijklmnopqrst
 Partition size : 4 
 abcde
 fghij
 klmno
 pqrst
 
 Given text : abcdefghijklmnopqrst
 Partition size : 5 
 abcd
 efgh
 ijkl
 mnop
 qrst
 
 Given text : abcdefghijklmnopqrst
 Partition size : 3 
 
 Equal partition not possible of size 3
// Scala program for
// Divide a string into n equal parts
class Partition()
{
	def nEqualPartition(text: String, n: Int): Unit = {
		// Get the length of given text
		var length: Int = text.length();
		// Display Given text
		print("\n Given text : " + text);
		// Display partition size
		print("\n Partition size : " + n + " \n ");
		if (length == 0 || n <= 0 || (length % n) != 0)
		{
			// When of partition not possible
			print("\n Equal partition not possible of size " + n);
		}
		else
		{
			// Get length of each part
			var part: Int = length / n;
			var i: Int = 0;
			// Display result
			while (i < length)
			{
				print(text.charAt(i));
				if ((i + 1) % part == 0)
				{
					// Separate the result by new line
					print("\n ");
				}
				i += 1;
			}
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Partition = new Partition();
		var text: String = "abcdefghijklmnopqrst";
		// partition n = 4
		task.nEqualPartition(text, 4);
		// partition n = 5
		task.nEqualPartition(text, 5);
		// partition n = 3
		task.nEqualPartition(text, 3);
	}
}

Output

 Given text : abcdefghijklmnopqrst
 Partition size : 4
 abcde
 fghij
 klmno
 pqrst

 Given text : abcdefghijklmnopqrst
 Partition size : 5
 abcd
 efgh
 ijkl
 mnop
 qrst

 Given text : abcdefghijklmnopqrst
 Partition size : 3

 Equal partition not possible of size 3
// Swift 4 program for
// Divide a string into n equal parts
class Partition
{
	func nEqualPartition(_ s: String, _ n: Int)
	{
      
      	let text = Array(s);
		// Get the length of given text
		let length: Int = text.count;
		// Display Given text
		print("\n Given text : ", s, terminator: "");
		// Display partition size
		print("\n Partition size : ", n ," \n ", terminator: "");
		if (length == 0 || n <= 0 || (length % n)  != 0)
		{
			// When of partition not possible
			print("\n Equal partition not possible of size ", n, terminator: "");
		}
		else
		{
			// Get length of each part
			let part: Int = length / n;
			var i: Int = 0;
			// Display result
			while (i < length)
			{
				print(text[i], terminator: "");
				if ((i + 1) % part == 0)
				{
					// Separate the result by new line
					print("\n ", terminator: "");
				}
				i += 1;
			}
		}
	}
}
func main()
{
	let task: Partition = Partition();
	let text: String = "abcdefghijklmnopqrst";
	// partition n = 4
	task.nEqualPartition(text, 4);
	// partition n = 5
	task.nEqualPartition(text, 5);
	// partition n = 3
	task.nEqualPartition(text, 3);
}
main();

Output

 Given text :  abcdefghijklmnopqrst
 Partition size :  4
 abcde
 fghij
 klmno
 pqrst

 Given text :  abcdefghijklmnopqrst
 Partition size :  5
 abcd
 efgh
 ijkl
 mnop
 qrst

 Given text :  abcdefghijklmnopqrst
 Partition size :  3

 Equal partition not possible of size  3
// Kotlin program for
// Divide a string into n equal parts
class Partition
{
	fun nEqualPartition(text: String, n: Int): Unit
	{
		// Get the length of given text
		val length: Int = text.length;
		// Display Given text
		print("\n Given text : " + text);
		// Display partition size
		print("\n Partition size : " + n + " \n ");
		if (length == 0 || n <= 0 || (length % n) != 0)
		{
			// When of partition not possible
			print("\n Equal partition not possible of size " + n);
		}
		else
		{
			// Get length of each part
			val part: Int = length / n;
			var i: Int = 0;
			// Display result
			while (i < length)
			{
				print(text.get(i));
				if ((i + 1) % part == 0)
				{
					// Separate the result by new line
					print("\n ");
				}
				i += 1;
			}
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Partition = Partition();
	val text: String = "abcdefghijklmnopqrst";
	// partition n = 4
	task.nEqualPartition(text, 4);
	// partition n = 5
	task.nEqualPartition(text, 5);
	// partition n = 3
	task.nEqualPartition(text, 3);
}

Output

 Given text : abcdefghijklmnopqrst
 Partition size : 4
 abcde
 fghij
 klmno
 pqrst

 Given text : abcdefghijklmnopqrst
 Partition size : 5
 abcd
 efgh
 ijkl
 mnop
 qrst

 Given text : abcdefghijklmnopqrst
 Partition size : 3

 Equal partition not possible of size 3


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







© 2021, kalkicode.com, All rights reserved