Divide a string in N equal parts | Java program to divide a string in n equal parts ; Method to print n equal parts of str ; Check if string can be divided in n equal parts ; Calculate the size of parts to find the division points ; Driver Code ; length od string is 28 ; Print 4 equal parts of the string
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 ) ; } }