Java > String-2 > repeatFront (CodingBat Solution)

Problem:

Given a string and an int n, return a string made of the first n characters of the string, followed by the first n-1 characters of the string, and so on. You may assume that n is between 0 and the length of the string, inclusive (i.e. n >= 0 and n <= str.length()).

repeatFront("Chocolate", 4) → "ChocChoChC"
repeatFront("Chocolate", 3) → "ChoChC"
repeatFront("Ice Cream", 2) → "IcI"


Solution:

public String repeatFront(String str, int n) {
  int len = str.length();
  String newWord = "";
  
  for (int i = n; n > 0; n--){
    newWord += str.substring(0,n);
  }
  return newWord;
}


20 comments :

  1. String result = "";

    for( ; n >= 0; n-- ) {
    result += str.substring(0, n);
    }
    return result;

    ReplyDelete
  2. String s="";
    for(int i=0;i<n;)
    {
    s = s + str.substring(i,n);
    n--;
    }
    return s;

    ReplyDelete
  3. String result = "";

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

    return result;

    ReplyDelete
  4. public String repeatFront(String str, int n) {
    String result="";
    int a =n;
    for(int i=0;i<a;i++){
    result+=str.substring(0,n);
    n--;
    }
    return result;
    }

    ReplyDelete
  5. public String repeatFront(String str, int n) {
    String c= "";

    for(int i=n;i>=0;i--) {

    c+=str.substring(0,i);
    }
    return c;

    }

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

    ReplyDelete
  7. public String repeatFront(String str, int n) {
    int len=str.length();
    String s="";
    for(n=n; n>=0; n--){

    s += str.substring(0,n);
    }
    return s;
    }

    ReplyDelete
  8. public String repeatFront(String str, int n) {
    if(n==4)
    {
    str=str.substring(0,4)+str.substring(0,3)+str.substring(0,2)+str.substring(0,1);
    return str;
    }
    else if(n==3)
    {
    str=str.substring(0,3)+str.substring(0,2)+str.substring(0,1);
    return str;
    }
    else if(n==2)
    {
    return str.substring(0,2)+str.substring(0,1);
    }
    else if(n==0)
    {
    return "";
    }
    else if(n==1)
    {
    return str.substring(0,1);
    }
    return "";
    }

    ReplyDelete
  9. String str2="";
    for(int i=n;i>=0;i--)
    {
    str2+=str.substring(0,i);
    }
    return str2;

    ReplyDelete
  10. public String repeatFront(String str, int n) {
    if (n < 1)
    {
    return "";
    }
    else
    {
    return str.substring(0, n) + repeatFront(str.substring(0, n - 1), n - 1);
    }
    }

    ReplyDelete
  11. public String repeatFront(String str, int n) {
    if (n == 0 || str.isEmpty()){
    return "";
    }
    return str.substring(0, n) + repeatFront(str, n - 1);
    }

    ReplyDelete
  12. public String repeatFront(String str, int n) {
    return (n == 0 || str.isEmpty())? "":
    str.substring(0, n) + repeatFront(str, n - 1);
    }

    ReplyDelete
  13. public String repeatFront(String str, int n) {
    String res="";
    while(n>0){
    res+=str.substring(0,n);
    n--;
    }
    return res;
    }

    ReplyDelete
  14. public String repeatFront(String str, int n)
    {
    String s = "";

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

    return s;
    }

    ReplyDelete
  15. public String repeatFront(String str, int n) {
    StringBuilder newStr = new StringBuilder();
    int count = n;

    for(int i = 0; i < n; i++){
    newStr.append(str.substring(0,count--));
    }

    return newStr.toString();
    }

    ReplyDelete
  16. public String repeatFront(String str, int n) {
    String sum="";
    str=str.substring(0,n);
    for(int i=0; i<str.length(); i++){
    String temp=str.substring(0,str.length()-i);
    sum+=temp;
    }
    return sum;
    }

    ReplyDelete
  17. public String repeatFront(String str, int n) {
    String newstr = str.substring(0, n);
    int j = 0;
    String k = "";
    for (int i = newstr.length(); i >= 0; i--) {
    for (int l = i; l > 0; l--) {

    k += newstr.charAt(j);
    j++;
    }
    j = 0;
    }
    return k;
    }

    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