The function below returns a substring of input string.
What is the invariant of this function?
Static string substring( String in, int ind) {
String t = in ;
t = t.substring(ind);
return t;
}
Answer:
Static string substring( String in, int ind) {
String t = in ;
t = t.substring(ind);
return t;
}
Here t.substring(ind) will return the string t from
begin_index = ind to last_index = in.lenght() , in.length() will
return the length of a string.
So here the invariant is the value of ind is greater than or eqaul
to 0 and less than len(string).Because the String index starts from
0 and ends at length_of_string - 1.
So ind value must be between the above range other than that values
it will raise an error(Index out of bound).
Invariant : 0 <= ind < in.lenght()
NOTE:
If You Have Any Doubts Feel Free To Comment In The
Comment Section.
Do Vote (LIKE).
Get Answers For Free
Most questions answered within 1 hours.