简体   繁体   中英

test cases for reversing a string

I am trying to practice for a QA interview and I want to handle as many test cases as I can think of. I believe my method works for every case except when the input is null I am not sure how to handle that exception. The compiler will give me an error first before the code even runs. So I am not sure how to deal with that exception. I read around and catch ing the NullPointerException is not recommended. How would I deal with handling a null input. Also if anyone else can think of other test cases, I would be happy to test and have a look at it!

Here are my test cases:

""
"abcdef"
"abbbba"
null

not quite sure how to handle

escape characters

"\n"  --> "n\"
"\t"  --> "t\"

special characters such as

áe  -->  eá 

Code:

public static String reverseStr(String str) {
    int len = str.length();
    if (len <= 0) {
        return "Empty String";
    }
    char[] strArr = new char[len];
    int count = 0;
    for (int i = len - 1; i >= 0; i--) {
        strArr[count] = str.charAt(i);
        count++;
    }
    return new String(strArr);
}

After further suggestions here is the updated code:

public static String reverseStr(String str) {
    if ( str == null ) {
          return null;
    }
    int len = str.length();
    if (len <= 0) {
        return "";
    }
    char[] strArr = new char[len];
    int count = 0;
    for (int i = len - 1; i >= 0; i--) {
        strArr[count] = str.charAt(i);
        count++;
    }
    return new String(strArr);
}

If string is null, the compiler will not give you an error, but you will get a NullPointerException when you run the code. Your reverseStr method can just check

if ( str == null ) {
  return "null string passed in";
}

Or you could do something like

int len = (str != null) ? str.length() : 0

And now both null and empty strings will have a length of 0. It depends if you want to treat null and empty differently.

对于null情况,您可以首先添加简单测试if(str==null) throw new IllegalArgumentException("arg must be not null")并向声明中添加throws IllegalArgumentException

The first thing you should do in the method is check the nullity:

public static String reverseStr(String str) {
    if(str==null){
        return null;
    }
    else{
       //the rest of your code goes here
    }
}

I don't understand why you return "Empty String" if the string is empty. That would imply that the input string was "gnirtS ytpmE" .

If the string is empty, you should return an empty string. If the string is null , you should probably return null (or throw an exception).

if (str == null) return null;
int len = str.length();
if (len == 0) return "";

The NPE(NullPointerException) is coming from accessing the parameter 'str' when it is null. It makes sense if you replace the parameter with 'null', so it would look like this: int len = null.length(); Which doesn't make a lot of sense. The '||' or operator differs from the '|' or operator in that if the first element is true, the other operand is not evaluated, since the whole result can be determined. Leveraging this in this situation allows you to safely call str.isEmpty() because it is guaranteed to not be null.

public static String reverseStr(String str) {
    if(null == str || str.isEmpty()){
        return "Empty String";
    }
    int len = str.length();

    char[] strArr = new char[len];
    int count = 0;
    for (int i = len - 1; i >= 0; i--) {
        strArr[count] = str.charAt(i);
        count++;
    }
    return new String(strArr);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM