Q:

Program to divide a string in 'N' equal parts

belongs to collection: String Programs

0

Here, our task is to divide the string S into n equal parts. We will print an error message if the string cannot be divisible into n equal parts otherwise all the parts need to be printed as the output of the program.

To check whether the string can be divided into N equal parts, we need to divide the length of the string by n and assign the result to variable chars.

If the char comes out to be a floating point value, we can't divide the string otherwise run a for loop to traverse the string and divide the string at every chars interval.

The algorithm of the program is given below.

Algorithm

  1. Define a string and define n, i.e., no. of equal parts that string needs to be divided in.
  2. No. of characters (variable chars) in each substring will be found out by dividing the length of the string by n.
  3. If the string cannot be divided into n equal parts, then display an error message.
  4. Else divide the string from i to chars (no. Of character)
  5. Then increment the count by chars and continue dividing the string till you get all the parts of the string.
  6. Print the count.

Complexity:

O(n)

Input:

 

str = "aaaabbbbcccc"  

Output:

Equal parts of given string are
aaaa
bbbb
cccc

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Python

str = "aaaabbbbcccc";  
#Stores the length of the string  
length = len(str);   
#n determines the variable that divide the string in 'n' equal parts  
n = 3;  
temp = 0;  
chars = int(length/n);  
#Stores the array of string  
equalStr = [];   
#Check whether a string can be divided into n equal parts  
if(length % n != 0):  
        print("Sorry this string cannot be divided into " + str(n) +" equal parts.");  
else:  
    for i in range(0, length, chars):  
        #Dividing string in n equal part using substring()  
        part = str[ i : i+chars];  
        equalStr.append(part);  
    print("Equal parts of given string are");  
    for i in equalStr:  
print(i);  

 

Output:

Equal parts of given string are 
aaaa
bbbb
cccc

 

C

#include <stdio.h>  
int main()  
{  
    char str[] = "aaabbbcccddd";  
    int i, counter, len = 0, n = 3, temp = 0, chars;  
    len = strlen(str);  
    chars = len/n;  
    char c[chars+1];  
    //Check whether a string can be divided into n equal parts  
    if(len % n != 0 || n == 0) {  
        printf("Sorry this string cannot be divided into %d equal parts.", n);  
    }  
    else {  
            printf("%d equal parts of given string are\n",n);  
            for(i = 0; i < len; i = i+chars) {  
                counter = 0;  
                //Dividing string in n equal part using substring()  
                while (counter < chars) {  
                    c[counter] = str[i + counter];  
                    counter++;  
                }  
                c[counter]='\0';  
                printf("%s",c);  
                printf("\n");  
            }  
        }  
    return 0;  
}  

 

Output:

3 equal parts of given string are 
aaaa
bbbb
cccc

 

JAVA

public class DivideString {  
    public static void main(String[] args) {  
          String str = "aaaabbbbcccc";  
          
        //Stores the length of the string  
        int len = str.length();  
        //n determines the variable that divide the string in 'n' equal parts  
        int n = 3;  
        int temp = 0, chars = len/n;  
        //Stores the array of string  
        String[] equalStr = new String [n];   
        //Check whether a string can be divided into n equal parts  
        if(len % n != 0) {  
            System.out.println("Sorry this string cannot be divided into "+ n +" equal parts.");  
        }  
        else {  
            for(int i = 0; i < len; i = i+chars) {  
                //Dividing string in n equal part using substring()  
                String part = str.substring(i, i+chars);  
                equalStr[temp] = part;  
                temp++;  
            }  
    System.out.println(n + " equal parts of given string are ");  
            for(int i = 0; i < equalStr.length; i++) {  
                System.out.println(equalStr[i]);  
                }  
            }  
        }  
}  

 

Output:

3 equal parts of given string are 
aaaa
bbbb
cccc

 

C#

using System;                      
public class Program  
{  
    public static void Main()  
    {  
        string str = "aaaabbbbcccc";  
        //Stores the length of the string  
        int len = str.Length;  
                      //n determines the variable that divide the string in 'n' equal parts  
        int n = 3;  
        int temp = 0, chars = len/n;  
                      //Stores the array of string  
        String[] equalStr = new String [n];   
                      //Check whether a string can be divided into n equal parts  
        if(len % n != 0) {  
            Console.WriteLine("Sorry this string cannot be divided into "+ n +" equal parts.");  
        }  
            else {  
            for(int i = 0; i < len; i = i+chars) {  
                  
                //Dividing string in n equal part using substring()  
                string part = str.Substring(i, chars);  
                equalStr[temp] = part;  
                temp++;  
            }  
            Console.WriteLine(n + " equal parts of given string are ");  
            for(int i = 0; i < equalStr.Length; i++) {  
                Console.WriteLine(equalStr[i]);  
            }  
        }  
    }  
}  

 

Output:

3 equal parts of given string are 
aaaa
bbbb
cccc

 

PHP

<!DOCTYPE html>  
<html>  
<body>   
<?php   
    $str = "aaaabbbbcccc";  
    //Stores the length of the string  
    $len = strlen($str);  
    //n determines the variable that divide the string in 'n' equal parts  
    $n = 3;  
    $temp = 0;  
    $chars = $len/$n;  
      
    //Stores the array of string  
    $equalStr = array();   
   
    //Check whether a string can be divided into n equal parts  
    if($len % $n != 0) {  
        echo "Sorry this string cannot be divided into ", $n ," equal parts.";  
        }  
    else {  
        for($i = 0; $i < $len; $i = $i+$chars) {  
              
            //Dividing string in n equal part using substring()  
            $part = substr($str, $i, $chars);  
            $equalStr[$temp] = $part;  
            $temp++;  
        }  
        echo $n , " equal parts of given string are <br>";  
        foreach($equalStr as $v){  
            echo "$v <br>";  
            }  
        }  
?>  
</body>  
</html>  

 

Output:

3 equal parts of given string are 
aaaa
bbbb
cccc

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


Program to find all the permutations of a string... >>
<< Program to determine whether two strings are the a...