.....
if(wrapstring.Length > n)
{
string tempwrapstring = "";
for(int i=0; i< i="i+n)"> wrapstring.Length)
tempwrapstring += wrapstring.Substring(i,(wrapstring.Length-i));
else
tempwrapstring += wrapstring.Substring(i, i+n) + "brtag";
}
wrapstring=tempwrapstring;
}
.........
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.
You need to identify the language. It could be any one of several object oriented languages.
ReplyDelete"tempwrapstring += wrapstring.Substring(i,(wrapstring.Length-i));"
ReplyDeleteThis 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);"
Well assume the language to be C#. But try finding the bug without executing the code.
ReplyDeleteSo if i=0 and (i+n > wrapstring.Length) then definitely the string will not be changed. Hence that is not the issue.
I dont know about the Substring function but the code has the following contradiction
ReplyDeletewrapstring.Substring(i,(wrapstring.Length-i))
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).
Timma, correct answer you got it right even though you dont know about Substring function. Good pattern matching.
ReplyDelete