public class Test {
static int count = 0;
public static void main(String[] args) {
ArrayList position = judge("wangyue","wanghua1");
System.out.println("不相同的 字符有:"+count+" 个");
System.out.print("不相同字母的位置:");
for(int i=0;i<position.size();i++){
System.out.print(position.get(i)+" ");
}
}
public static ArrayList judge(String str1, String str2) {
ArrayList position =new ArrayList();
int len1 = str1.length();
int len2 = str2.length();
for (int i=0;i<(len1>len2?len2:len1);i++) {
char c1 = str1.charAt(i);
char c2 = str2.charAt(i);
if (c1 != c2) {
count++;
position.add(i);
}
}
return position;
}
}
这是我的代码,,但是如果两个字符串长度不一样的时候 就不好使了
|
|