Skip to content

Latest commit

 

History

History
25 lines (19 loc) · 577 Bytes

repeatEnd.md

File metadata and controls

25 lines (19 loc) · 577 Bytes

repeatEnd

Given a string and an int n, return a string made of n repetitions of the last n characters of the string. You may assume that n is between 0 and the length of the string, inclusive.

repeatEnd("Hello", 3) → "llollollo"
repeatEnd("Hello", 2) → "lolo"
repeatEnd("Hello", 1) → "o"

Solution:

public String repeatEnd(String str, int n) {
  String str2 = "";
  for(int i=0; i<n; i++){
    str2 += str.substring(str.length()-n);
  }
  return str2;
}

codingbat

< back to readme