Hi,
I was given following question as an assessment but unfortunately I was unable to answer. Can someone help ?
The question is :
The function given below is supposed to retrieve the substring between and including the ith and jth characters of the input string s. When i and j are out of range, ie. i,j< 0 ori,j >= s.length(), the function returns the string "
| Test case | s | i | j | Expected return value | Actual return value |
| 1 | "quick" | 0 | 2 | "qui" | "qui" |
| 2 | "brown" | 2 | 1 | "ro" | "" |
| 3 | "fox" | 1 | 9 | " | " |
| 4 | "jumped" | -1 | 4 | " | " |
| 5 | "lazy" | 3 | 0 | "lazy" | " |
| 6 | "dog" | 1 | 1 | "o" | "o" |
Unfortunately, the function returns the wrong value for the 2nd and 5thtest cases.
C# Version:
string SubString(string s, int i, int j)
{
int k;
string result;
try
{
k = s.Length;
if (i < 0 || j < 0 || i >= k || j >= k)
throw new Exception();
result = s.Substring(i, j + 1 - i);
}
catch (Exception e)
{
return "
}
return result;
}
a) Under what general conditions does the function return the wrong value?
b) Add some code to fix the function. Can you improve it? If so explain why your version is better.
Rahul ChavanPosted Apr 13, 2016, 3:05 AM