File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * PROGRAM : To divide a string into N equal parts.
3
+ * FILE : DivideString.java
4
+ * CREATED BY : Santosh Hembram
5
+ * DATED : 14-10-2020
6
+ */
7
+ import java .util .*;
8
+ class DivideString {
9
+
10
+ public static void main (String [] args ) {
11
+
12
+ String str ;
13
+ System .out .print ("Enter a string:" );
14
+ Scanner sc = new Scanner (System .in );
15
+ str = sc .nextLine ();
16
+
17
+ System .out .print ("Enter the number of parts to be divided: " );
18
+ int n = sc .nextInt ();
19
+
20
+ int str_len = str .length ();
21
+
22
+ int div = str_len /n ;
23
+ int temp =0 ;
24
+ String [] arrStr = new String [n ];
25
+
26
+ if (str_len % n != 0 ) {
27
+
28
+ System .out .println ("----- This string cannot be divide into " +n +" equal parts -----" );
29
+
30
+ }
31
+ else {
32
+
33
+ for (int i =0 ; i <str_len ; i =i +div ) {
34
+
35
+ String newStrData = str .substring (i ,i +div );
36
+ arrStr [temp ] = newStrData ;
37
+ temp ++;
38
+ }
39
+ System .out .println (n +" equal parts of given string are: " );
40
+ for (int i =0 ; i <arrStr .length ; i ++) {
41
+ System .out .println (arrStr [i ]);
42
+ }
43
+ }
44
+ }
45
+ }
You can’t perform that action at this time.
0 commit comments