-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFRONT_TIMES.java
More file actions
36 lines (32 loc) · 934 Bytes
/
FRONT_TIMES.java
File metadata and controls
36 lines (32 loc) · 934 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
/*
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;
frontTimes("Chocolate", 2) → "ChoCho"
frontTimes("Chocolate", 3) → "ChoChoCho"
frontTimes("Abc", 3) → "AbcAbcAbc
*/
package school;
public class FRONT_TIMES {
public static void main(String [] args){
String answer = frontTimes("Chocolate", 2);
System.out.println(answer);
}
public static String frontTimes(String str, int n) {
int i = 0;
String answer = "";
if(str.length()<=3){
while(i<n){
answer = answer + str;
i++;
}
}else{
while(i<n){
String result = str.substring(0,3);
answer = answer + result;
i++;
}
}
return answer;
}
}