Difficulty Level: Rookie Question: Write a program to print N equal parts of a given string. Solution: 1) Get the size of the string using string function strlen() (present in the string.h) 2) Get the size of a part.
part_size = string_length/n
3) Loop through the input string. In loop, if index becomes multiple of part_size then put a part separator(“\n”) Implementation:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
print"Invalid Input: String size is not divisible by n"
return
# Calculate the size of parts to find the division points
part_size =str_size/n
k =0
fori instring:
ifk %part_size ==0:
print"\n",
printi,
k +=1
# Driver program to test the above function
# Length of string is 28
string ="a_simple_divide_string_quest"
# Print 4 equal parts of the string
divideString(string, 4)
# This code is contributed by Bhavya Jain
C#
// C# program to divide a string
// in n equal parts
usingSystem;
classGFG {
// Method to print n
// equal parts of str
staticvoiddivideString(String str, intn)
{
intstr_size = str.Length;
intpart_size;
// Check if string
// can be divided in
// n equal parts
if(str_size % n != 0) {
Console.Write("Invalid Input: String size"
+ "is not divisible by n");
return;
}
// Calculate the size
// of parts to find
// the division points
part_size = str_size / n;
for(inti = 0; i < str_size; i++) {
if(i % part_size == 0)
Console.WriteLine();
Console.Write(str[i]);
}
}
// Driver Code
staticvoidMain()
{
// length od string is 28
String str = "a_simple_divide_string_quest";
// Print 4 equal parts of the string
divideString(str, 4);
}
}
// This code is contributed by Anuj_67
PHP
<?php
// PHP program to divide a string
// in n equal parts
// Function to print n
// equal parts of str
functiondivideString($str, $n)
{
$str_size= strlen($str);
$i;
$part_size;
// Check if string can be divided
// in n equal parts
if($str_size% $n!= 0)
{
echo"Invalid Input: String size";
echo" is not divisible by n";
return;
}
// Calculate the size of parts to
// find the division point
$part_size= $str_size/ $n;
for($i= 0; $i< $str_size; $i++)
{
if($i% $part_size== 0)
echo"\n";
echo$str[$i];
}
}
// Driver Code
// length od string is 28
$str= "a_simple_divide_string_quest";
// Print 4 equal parts of the string
divideString($str, 4);
// This code is contributed by ajit.
?>
Javascript
<script>
// Javascript program to divide a string
// in n equal parts
// Method to print n
// equal parts of str
functiondivideString(str, n)
{
let str_size = str.length;
let part_size;
// Check if string
// can be divided in
// n equal parts
if(str_size % n != 0)
{
document.write("Invalid Input: String size"+
"is not divisible by n");
return;
}
// Calculate the size
// of parts to find
// the division points
part_size = parseInt(str_size / n, 10);
for(let i = 0; i< str_size; i++)
{
if(i % part_size == 0)
document.write("</br>");
document.write(str[i]);
}
}
// length od string is 28
let str = "a_simple_divide_string_quest";
// Print 4 equal parts of the string
divideString(str, 4);
// This code is contributed by rameshtravel07
</script>
Output
a_simpl
e_divid
e_strin
g_quest
In the above solution, n equal parts of the string are only printed. If we want individual parts to be stored, we need to allocate part_size + 1 memory for all N parts (1 extra for string termination character ‘\0’) and store the addresses of the parts in an array of character pointers.
Another Approach: Using STL in C++
C++
#include <iostream>
usingnamespacestd;
voiddivide(string str, intn)
{
if(str.length() % n != 0) {
cout << "Invalid Input: String size";
cout << " is not divisible by n";
return;
}
intparts = str.length() / n;
intstart = 0;
while(start < str.length()) {
cout << str.substr(start, parts) << endl;
start += parts;
// if(start < str.length()) cout << endl; to ignore
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy