2013-10-28 作业
1.定义一char数组字符,然后判断是否为回文。
import java.util.Scanner;
public class judgeHuiwen{
public static void main(String args[]){
char[] array;
char[] comparearray;
int i,j;
String myString;
int charlength;
System.out.println("input array :");
Scanner s = new Scanner(System.in);
myString=s.next();
charlength=myString.getBytes().length;
comparearray=new char[charlength];
array=new char[charlength];
for(i=0;i<charlength;i++){
array[i] = myString.charAt(i);
}
for(j=charlength-1;j>=0;j--){
comparearray[charlength-1-j]=array[j];
}
for(i=0;i<charlength;i++){
if(comparearray[i]!=array[i]){
System.out.println("此字符数组不是回文");
break;
}
if(i==charlength-1){
System.out.println("此字符数组是回文");
}
}
}
}
2.定义一char数组字符,然后将字符倒置输出。
import java.util.Scanner;
public class inversionChar{
public static void main(String args[]){
char[] array;
char[] inversionarray;
int i,j;
String myString;
int charlength;
System.out.println("input array :");
Scanner s = new Scanner(System.in);
myString=s.next();
charlength=myString.getBytes().length;
inversionarray=new char[charlength];
array=new char[charlength];
for(i=0;i<charlength;i++){
array[i] = myString.charAt(i);
}
for(j=charlength-1;j>=0;j--){
inversionarray[charlength-1-j]=array[j];
}
System.out.println("output inversion array :");
for(i=0;i<charlength;i++){
System.out.print(inversionarray[i]);
}
}
}