Java > String-2 > repeatEnd (CodingBat Solution)

Problem:

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) {
  int len = str.length();
  String newWord = "";
  
  for (int i = 0; i < n; i++) {
    newWord += str.substring(len - n, len);
  }
  return newWord;
}


19 comments :

  1. public String repeatEnd(String str, int n) {
    String result = "";

    for(int i=0; i<str.length() && i<n; i++)
    {
    result+= str.substring(str.length()-n);

    }


    return result;
    }

    ReplyDelete
  2. public String repeatEnd(String str, int n) {
    int len = str.length();
    String str1 = "";

    for (int i = 0; i < n ; i++)
    {
    str1 = str1+ str.substring(len-n, len);
    }
    return str1;
    }

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

    ReplyDelete
  4. public String repeatEnd(String str, int n) {
    String s1="";
    String s2="";
    int size=n;
    for(int i=0;i<n;i++)
    {
    s1=s1+str.charAt(str.length()-size);
    size--;
    }
    for(int j=0;j<n;j++)
    {
    s2=s1+s2;
    }
    return s2;
    }

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

    ReplyDelete
  6. public String repeatEnd(String str, int n) {
    String sol;
    if(n==0)
    {
    return "";
    }
    else if(n==3)
    {
    sol=str.substring(str.length()-3)+str.substring(str.length()-3)+
    str.substring(str.length()-3);
    return sol;
    }

    else if(n==2)
    {
    sol=str.substring(str.length()-2)+str.substring(str.length()-2);
    return sol;
    }
    return str.substring(str.length()-1);
    }

    ReplyDelete
  7. String str2="";
    for(int j=0;j<n;j++)
    {
    str2+=str.substring(str.length()-n,str.length());
    }
    return str2;

    ReplyDelete
  8. public String repeatEnd(String str, int n) {
    String newWord = "";

    for(int i = 0; i < n; i++){
    newWord = newWord + str.substring(str.length()-n);
    }
    return newWord;
    }

    ReplyDelete
  9. String d="";
    for(int i=0;i<n;i++){
    d+=str.substring(str.length()-n);
    }
    return d;
    }

    ReplyDelete
  10. public String repeatEnd(String str, int n) {
    if(n == 0 ){
    return "";
    }
    return new String(new char[n]).replace("\0", str.substring(str.length()-n));
    }

    ReplyDelete
  11. public String repeatEnd(String str, int n) {
    return (n == 0 )? ""
    :new String(new char[n]).replace("\0", str.substring(str.length()-n));
    }

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

    }

    ReplyDelete
  13. Laura lassoon pyaaz mirchi!


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

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

    ReplyDelete
  15. public String repeatEnd(String str, int n) {

    String res = "";
    int len = str.length()-n;
    for(int i = 0; i < n; ++i) {

    res += str.substring(len);
    }

    return res;
    }

    ReplyDelete
  16. public String repeatEnd(String str, int n)
    {
    String s = "";

    for(int i = 0; i < n; i++)
    {
    s += str.substring(str.length()-n,str.length());
    }

    return s;
    }

    ReplyDelete
  17. public String repeatEnd(String str, int n) {

    String result = "";
    int i = n;

    while(i != 0)
    {
    result += str.substring(str.length()-n, str.length());
    i--;
    }

    return result;
    }

    ReplyDelete
  18. Another way

    public String repeatEnd(String str, int n) {
    return String.join("", Collections.nCopies(n, str.substring(str.length() - n)));
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact