Divide a string in N equal parts
Last Updated :
26 Apr, 2023
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:
C
#include <stdio.h>
#include <string.h>
void divideString( char * str, int n)
{
int str_size = strlen (str);
int i;
int part_size;
if (str_size % n != 0) {
printf ( "Invalid Input: String size" );
printf ( " is not divisible by n" );
return ;
}
part_size = str_size / n;
for (i = 0; i < str_size; i++) {
if (i % part_size == 0)
printf ( "\n" );
printf ( "%c" , str[i]);
}
}
int main()
{
char * str = "a_simple_divide_string_quest" ;
divideString(str, 4);
getchar ();
return 0;
}
|
C++
#include <iostream>
#include <string.h>
using namespace std;
class gfg {
public :
void divideString( char str[], int n)
{
int str_size = strlen (str);
int i;
int part_size;
if (str_size % n != 0) {
cout << "Invalid Input: String size" ;
cout << " is not divisible by n" ;
return ;
}
part_size = str_size / n;
for (i = 0; i < str_size; i++) {
if (i % part_size == 0)
cout << endl;
cout << str[i];
}
}
};
int main()
{
gfg g;
char str[] = "a_simple_divide_string_quest" ;
g.divideString(str, 4);
return 0;
}
|
Java
class GFG {
static void divideString(String str, int n)
{
int str_size = str.length();
int part_size;
if (str_size % n != 0 ) {
System.out.println( "Invalid Input: String size"
+ "is not divisible by n" );
return ;
}
part_size = str_size / n;
for ( int i = 0 ; i < str_size; i++) {
if (i % part_size == 0 )
System.out.println();
System.out.print(str.charAt(i));
}
}
public static void main(String[] args)
{
String str = "a_simple_divide_string_quest" ;
divideString(str, 4 );
}
}
|
Python3
def divideString(string, n):
str_size = len (string)
if str_size % n ! = 0 :
print ( "Invalid Input: String size is not divisible by n" )
return
part_size = str_size / n
k = 0
for i in string:
if k % part_size = = 0 :
print ()
print (i,end = '')
k + = 1
string = "a_simple_divide_string_quest"
divideString(string, 4 )
|
C#
using System;
class GFG {
static void divideString(String str, int n)
{
int str_size = str.Length;
int part_size;
if (str_size % n != 0) {
Console.Write( "Invalid Input: String size"
+ "is not divisible by n" );
return ;
}
part_size = str_size / n;
for ( int i = 0; i < str_size; i++) {
if (i % part_size == 0)
Console.WriteLine();
Console.Write(str[i]);
}
}
static void Main()
{
String str = "a_simple_divide_string_quest" ;
divideString(str, 4);
}
}
|
PHP
<?php
function divideString( $str , $n )
{
$str_size = strlen ( $str );
$i ;
$part_size ;
if ( $str_size % $n != 0)
{
echo "Invalid Input: String size" ;
echo " is not divisible by n" ;
return ;
}
$part_size = $str_size / $n ;
for ( $i = 0; $i < $str_size ; $i ++)
{
if ( $i % $part_size == 0)
echo "\n" ;
echo $str [ $i ];
}
}
$str = "a_simple_divide_string_quest" ;
divideString( $str , 4);
?>
|
Javascript
<script>
function divideString(str, n)
{
let str_size = str.length;
let part_size;
if (str_size % n != 0)
{
document.write( "Invalid Input: String size" +
"is not divisible by n" );
return ;
}
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]);
}
}
let str = "a_simple_divide_string_quest" ;
divideString(str, 4);
</script>
|
Output
a_simpl
e_divid
e_strin
g_quest
Time Complexity: O(n), where n is the length
C
#include <stdio.h>
#include <string.h>
void divide( char * str, int n)
{
if ( strlen (str) % n != 0) {
printf ( "Invalid Input: String size is not "
"divisible by n\n" );
return ;
}
int parts = strlen (str) / n;
int start = 0;
while (start < strlen (str)) {
char substr[parts + 1];
strncpy (substr, str + start, parts);
substr[parts] = '\0' ;
printf ( "%s\n" , substr);
start += parts;
}
}
int main()
{
char str[] = "a_simple_divide_string_quest" ;
divide(str, 4);
return 0;
}
|
C++
#include <iostream>
using namespace std;
void divide(string str, int n)
{
if (str.length() % n != 0) {
cout << "Invalid Input: String size" ;
cout << " is not divisible by n" ;
return ;
}
int parts = str.length() / n;
int start = 0;
while (start < str.length()) {
cout << str.substr(start, parts) << endl;
start += parts;
}
}
int main()
{
string str = "a_simple_divide_string_quest" ;
divide(str, 4);
}
|
Java
import java.util.*;
class GFG {
static void divide(String str, int n) {
if (str.length() % n != 0 ) {
System.out.print( "Invalid Input: String size" );
System.out.print( " is not divisible by n" );
return ;
}
int parts = str.length() / n;
int start = 0 ;
int t = parts;
while (start < str.length()) {
String temp = new String(str);
System.out.print(temp.substring(start, parts) + "\n" );
start = parts;
parts += t;
}
}
public static void main(String[] args) {
String str = "a_simple_divide_String_quest" ;
divide(str, 4 );
}
}
|
Python3
def divide( Str ,n):
if ( len ( Str ) % n ! = 0 ):
print ( "Invalid Input: String size" ,end = "")
print ( " is not divisible by n" )
return
parts = len ( Str ) / / n
start = 0
while (start < len ( Str )):
print ( Str [start: start + parts])
start + = parts
Str = "a_simple_divide_string_quest"
divide( Str , 4 )
|
C#
using System;
public class GFG {
public static void divide(String str, int n)
{
if (str.Length % n != 0) {
Console.Write( "Invalid Input: String size" );
Console.Write( " is not divisible by n" );
return ;
}
var parts = ( int )(str.Length / n);
var start = 0;
var t = parts;
while (start < str.Length) {
var temp = new String(str);
Console.Write(
temp.Substring(start, parts - start)
+ "\n" );
start = parts;
parts += t;
}
}
public static void Main(String[] args)
{
var str = "a_simple_divide_String_quest" ;
divide(str, 4);
}
}
|
Javascript
<script>
function divide(str,n)
{
if (str.length % n != 0) {
document.write( "Invalid Input: String size" );
document.write( " is not divisible by n" );
return ;
}
let parts = Math.floor(str.length / n);
let start = 0;
while (start < str.length) {
document.write(str.substring(start, start+parts), "</br>" );
start += parts;
}
}
let str = "a_simple_divide_string_quest" ;
divide(str, 4);
</script>
|
of string.
Auxiliary Space: O(1).
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++
Implemention:
Output
a_simpl
e_divid
e_strin
g_quest
Time Complexity: O(n), where n is the length of string.
Auxiliary Space: O(n), because when string is passed to the function it creates a copy of itself (passing by value).
Plein case write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Share your thoughts in the comments
Please Login to comment...