Find the Bug - 2

Continuing with the series "Find the Bug", here is the next code snippet

if(wrapstring.Length > n)
string tempwrapstring = "";
for(int i=0; i< i="i+n)"> wrapstring.Length)
tempwrapstring += wrapstring.Substring(i,(wrapstring.Length-i));
tempwrapstring += wrapstring.Substring(i, i+n) + "brtag";

The codesnippet is about wrapping the strings of length greater than n into multi-line strings using brtag(Line break in HTML). What is the bug in the above code snippet. What is the solution. Add your answers in the comments.


  1. Anonymous10:31 AM

    You need to identify the language. It could be any one of several object oriented languages.

  2. Anonymous3:29 PM

    "tempwrapstring += wrapstring.Substring(i,(wrapstring.Length-i));"

    This statment for i=0 will be
    "tempwrapstring += wrapstring.Substring(0,(wrapstring.Length-0));"

    Means the entire string. No changes in the string.
    it should be
    "tempwrapstring += wrapstring.Substring(i,n);"

  3. Well assume the language to be C#. But try finding the bug without executing the code.

    So if i=0 and (i+n > wrapstring.Length) then definitely the string will not be changed. Hence that is not the issue.

  4. Kunduru Timma Reddy11:52 AM

    I dont know about the Substring function but the code has the following contradiction

    wrapstring.Substring(i, i+n)

    The signature of first call is specifying that substring will extract x(2nd argument) chars starting from position i(1st argument).

    The signature of second call is specifying that substring will extract characters from position a(1st argument) to position b(2nd argument).

  5. Timma, correct answer you got it right even though you dont know about Substring function. Good pattern matching.