c# - return string concatenated with itself, without exceeding limit

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sskofid11
    New Member
    • Jul 2017
    • 5

    c# - return string concatenated with itself, without exceeding limit

    Hi,

    I have attempted to answer this question but where have I gone wrong?

    Return the given string doubled (concatenated with itself) , it should be doubled as many times as possible without exceeding the length given by limit. The string should be doubled a minimum of once, regardless of limit.




    Code:
            public static string DoubleString(string aString, int limit)
            {
    
    
                do
                {
                   string.Concat(aString, aString);
                } while (aString.Length <= limit);
                return aString;
    
    
    
            }
        }
  • Luk3r
    Contributor
    • Jan 2014
    • 300

    #2
    This code will literally concatenate the string onto itself. (Example: If the string is 'test' and the limit is 2, the result will be 'testtest'). You were very close.

    Code:
            public static string DoubleString(string aString, int limit)
            {
                limit = aString.Length * limit;
                do
                {
                    aString += aString;
                } while (aString.Length < limit);
                return aString;
            }

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Luk3r, you're close but in the end aString will be greater than the limit because it needs to be longer than the limit in order to exit your while.

      You could do this:
      Code:
      public static string DoubleString(string aString, int limit)
      {
        int doublingLimit = aString.Length * limit;
        string result = aString;
        do
        {
          result += result ;
        } while ((result + result).Length < doublingLimit);
        return result ;
      }
      Or this:
      Code:
      public static string DoubleString(string aString, int limit)
      {
        int doublingLimit = aString.Length * limit;
        string result = "";
        string check = aString;
        do
        {
          result = check;
          check += aString;
        } while (check).Length < doublingLimit);
        return result ;
      }
      However, I'm not really sure why we are doing any calculation with limit...the limit is a hard value according to the specs: ...without exceeding the length given by limit.

      So it really should be:
      Code:
      public static string DoubleString(string aString, int limit)
      {
        string result = aString;
        do
        {
          result += result ;
        } while ((result + result).Length < limit);
        return result ;
      }
      Or this:
      Code:
      public static string DoubleString(string aString, int limit)
      {
         string result = "";
        string check = aString;
        do
        {
          result = check;
          check += aString;
        } while (check).Length < limit);
        return result ;
      }
      Unless the value provided as limit is really the number of times it can be doubled...in which case we could simply do:
      Code:
      public static string DoubleString(string aString, int limit)
      {
        string result = "";
        int counter = 0;
        do
        {
          result = check;
          check += aString;
        } while (counter < limit);
        return result ;
      }
      But I really don't think this is the intention because of the specs provided.


      Regardless, my point is that you cannot use the value that you will be returning to check against the limit as a condition to exit the while loop because it must be larger than the limit in order to exit the loop which is not what you want. You really want the value you are returning to be 1 iteration less than the exit condition.

      -Frinny

      Comment

      Working...