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:
-
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:
-
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:
-
public static string DoubleString(string aString, int limit)
-
{
-
string result = aString;
-
do
-
{
-
result += result ;
-
} while ((result + result).Length < limit);
-
return result ;
-
}
-
Or this:
-
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:
-
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