TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to Divide a String in 'N' Equal Parts

Program to Divide a String in 'N' Equal Parts on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

<< Back to PROGRAM

Q. Program to divide a string in 'N' equal parts.

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)

Solution

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

Next TopicPrograms List




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf