-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrontTimes.java
More file actions
42 lines (35 loc) · 864 Bytes
/
frontTimes.java
File metadata and controls
42 lines (35 loc) · 864 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
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;
eg:
frontTimes("Chocolate", 2) → "ChoCho"
frontTimes("Chocolate", 3) → "ChoChoCho"
frontTimes("Abc", 3) → "AbcAbcAbc"
*/
public String frontTimes(String str, int n) {
/*
String front;
String result = "";
if(str.length()<3){
for(int i=0; i<n; i++){
result += str;
}
}else{
front = str.substring(0,3);
for(int i=0; i<n; i++){
result += front;
}
}
return result;
*/
int frontLen = 3;
String front;
String result = "";
if (frontLen > str.length()){
frontLen = str.length();
}
front = str.substring(0, frontLen);
for(int i =0; i<n; i++){
result += front;
}
return result;
}