forked from mirandaio/codingbat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrontTimes.java
More file actions
25 lines (21 loc) · 754 Bytes
/
frontTimes.java
File metadata and controls
25 lines (21 loc) · 754 Bytes
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
/* Given a string and a non-negative int n, we'll say that the front of the
* string is the first 3 chars, or whatever is there if the string is less
* than length 3. Return n copies of the front.
*/
public String frontTimes(String str, int n) {
char[] result;
String front;
if(str.length() < 3)
front = str;
else
front = str.substring(0, 3);
result = new char[n * front.length()];
int index = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < front.length(); j++) {
result[index] = front.charAt(j);
index++;
}
}
return new String(result);
}