此代码从用户那里获取一个输入字符串,其中可能包含特殊字符。然后我们输出字母表中缺少的字母。例如,“the quick brown fox jumped over the lazy dog”将返回一个空字符串,但“ZYXW, vu TSR Ponm lkj ihgfd CBA”。会缺少字母“eq”。
目前,我的程序正在返回整个字母表,而不仅仅是丢失的字符。
import java.io.*;
import org.apache.commons.lang3.*;
public class QuickBrownFox {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = reader.readLine();
s = s.toUpperCase();
String[] arr = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
"T", "U", "V", "W", "X", "Y", "Z" };
String chars_only = "";
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j < arr.length; j++) {
if (s.substring(i, i + 1).equals(arr[j])) {
chars_only += s.substring(i, i + 1);
}
}
}
System.out.println(chars_only); // now we have a string of only alphabet letters
String missing = "";
for (int j = 0; j < arr.length; j++) {
if (StringUtils.contains(arr[j], chars_only) == false) { // alphabet letter not found
missing += arr[j];
}
}
missing = missing.toLowerCase();
System.out.println("missing letters: " + missing);
}
}