判断A字符串是B字符串的反转

时间:2022-07-24 10:31:20

先将其中一个字符串进行反转操作,然后两个字符串进行判断.

1.反转

    /**
* 字符串反转
* @param str
* @return
*/
private static String convert(String str){
char[] charArray = str.toCharArray();
int len = charArray.length;
char[] des = new char[len];
for(int i = 0; i < len; i++){
des[len - i -1] = charArray[i];
}
return String.valueOf(des);
}

2.判断

    /**
* 判断两个字符串是否互为反转字符串
* @param a
* @param b
* @return
*/
private static boolean isReverse(String a,String b){
String str = convert(a);
return b.equals(str);
}

3.测试

    public static void main(String[] args){        

        String a = "abcdef";
String b = "fedcba";
String c = "abcdfe";
System.out.println(isReverse(a,b));//true
System.out.println(isReverse(a,c));//false
}