Given a string and a non-negative int n, return a larger string that is n copies of the original string.
stringTimes("Hi", 2) → "HiHi"stringTimes("Hi", 3) → "HiHiHi"
stringTimes("Hi", 1) → "Hi"
Hisoka :
1 2 3 4 5 6 7 8 9 | public String stringTimes(String str, int n) { String result=""; while(n>0) { result += str; n -= 1; } return result; } |
Output :
Codingbat:
1 2 3 4 5 6 7 | public String stringTimes(String str, int n) { String result = ""; for (int i=0; i<n; i++) { result = result + str; // could use += here } return result; } |
No comments:
Post a Comment