java 扫描输入的字符串,统计Z O J 7的数量

3. Sort ZOJ7
Given a string of no more than 1000 characters. You are supposed to sort the characters into a substring of all Z's followed by O's, J's, 7's, and the rest of the other characters.
Input
Each case gives a string described by the problem. The string given contains no space.
Output
For each test case, output the resulting string. Note the order of the characters other than Z, O, J and 7 must be kept after sorting.
Sample Input
t7ZJ7OhO7B7O7irZtOhZdayJ77
Sample Output
ZZZOOOOJJ7777777thBirthday
解题思路:扫描输入的字符串,统计Z O J 7的数量,并且把不是ZOJ7的其它字符放到另一个数组中(需记录个数),输出时,按ZOJ7的数量循环输出单个字符,最后根据另一个数组中的个数,循环输出。

public class StringTest {
public static void main(String[] args) {
String str="t7zj7oho7b7o7irztohzdayj77".toLowerCase(),s="ZOJ7".toLowerCase(),s1="",s2="";
for(int i=0;i<str.length();i++) {
String tem=""+str.charAt(i);
if(!(s.contains(tem)))
s1+=str.charAt(i);
}
for(int i=0;i<s.length();i++) {
char c1=s.charAt(i);
for (int j = 0; j < str.length(); j++) {
char c2=str.charAt(j);
if(c1==c2)
s2+=c2;
}
}
System.out.println(s2+"  非重复:"+s1+" 个数: "+s1.length()+"\n\n["+s2+s1+"]");
}

温馨提示:答案为网友推荐,仅供参考
相似回答