Microsoft Interview Question
Software Engineer / DevelopersI corrected it as
public int getchar(string s)
{
if(s!=null)
if(s.length()>4)
char c=s[3];
}
public int getchar(string s) {
.....int returnValue = -1;
.....if(s == null) return returnValue;
.....if(s.length() >= 4) {
..........char c = s[3];
..........returnValue = (int)c;
.....}
.....return returnValue;
}
4 issues
- morpheus February 12, 2009(1) no return value. ( should return a char - as the method name suggests so).
(2) Cannot assign a char to string variable
(3) no error checking for incoming string ( what if s is null?)
(4) if we need 3rd char of the string, we should check if the string has atleast 3 chars.