-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathReshape_string.java
More file actions
54 lines (45 loc) · 1.39 KB
/
Copy pathReshape_string.java
File metadata and controls
54 lines (45 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package reshape_string;
import java.util.Scanner;
/**
*
* @author JOSEPH
*
*/
public class Reshape_string {
/*
Statement
The reshape(n, str) method should return the string str without spaces and layed out in lines of at most n characters.
Examples:
reshape(3, "abc de fghij") => returns "abc\ndef\nghi\nj"
reshape(6, "1 23 456") => returns "123456"
*/
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String str;
int num;
//insert a string and a number
System.out.print("Insert a string: ");
str = input.nextLine();
System.out.print("Insert the number of character: ");
num = input.nextInt();
//print the result of the reshape method
System.out.println("Result of reshape method: " +'\n' +reshape(num, str));
}
public static String reshape(int n, String str) {
//replace each space with empty string
str = str.replace(" ", "");
//insert a '\n' character each n characters
String res = "";
for (int i = 0; i < str.length(); i++)
{
if (i%n == 0 && i != 0)
res = res +'\n' +str.charAt(i);
else
res += str.charAt(i);
}
return res;
}
}