Skip to content

Commit 0cf028c

Browse files
authored
Add files via upload
1 parent 1a497fc commit 0cf028c

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

Assignment-10/DivideString.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}

0 commit comments

Comments
 (0)