public class PalindromeTest {
public static void main(String[] args) {
isPalidrome("abcdedcba") ;
}
public static void isPalidrome(String str){
char[] ch = str.toCharArray();
int len = ch.length;
for (int i = 0, int j = len - 1; i <= j;) {
if (ch[i++] == ch[j--]) {
} else {
System.out.println("this string is not palindrome!");
break;
}
}
}
}
另外,Java实现逆序输出:
public class InverseString {
public static void main(String[] args) {
String str = "fdsadsasssggg";
System.out.println(inverseString(str));
}
public static String inverseString(String s) {
char[] a = new char[s.length()];
char[] temp = s.toCharArray();
for (int i = temp.length-1,j=0; i >=0;) {
a[j++] = temp[i--];
}
return String.valueOf(a);
}
}