假设我输入以下字符串:
" Hello party"
您怎么能只对该链中的元音进行排序,而是以降序的方式排序呢?
举个例子:
"Halle porty"
我试着用下面的方法来解决这个问题,但是当我运行它的时候,它告诉我我有一个ExeptionBounce 5,我该怎么纠正它呢?我还被告知,责任如下:
if(cad[i].equalsIgnoreCase(vocal[j])
private String[]cad;
private String[]vocal={"a","e","i","o","u"};
private String resp="";
private String aux="":
private String []sort;
private int cont=0;
public String ordenar(String cadena)
{
cad= cadena.split("");
for(int i=0; i<cad.legth; i++)
{
for(int j=i, j<vocal.legth; j++)
{
if(cad[i].equalsIgnoreCase(vocal[j])
{
aux+=cad[i];
sort= aux.Split("");
Arrays.sort(sort);
for(int i=0; i<cad.legth; i++)
{
cad[i] = sort[k];
resp+=cad[i];
}
}
}
return resp;
}
发布于 2019-05-29 07:01:02
你的代码甚至不能编译--你是如何运行它的?
为了回答你的问题,这里有一段代码,给定一个字符串,它将返回经过元音排序的字符串:
public static boolean isVowel(char c) {
return "AEIOUaeiou".indexOf(c) != -1;
}
public static String sortVowelsOnly(String input) {
if (input == null) {
return input;
}
StringBuilder result = new StringBuilder(input);
//keep track at all positions where we saw a vowel
ArrayList<Integer> vowelPositions = new ArrayList<>();
//keep track of all vowels we saw so far
ArrayList<Character> vowels = new ArrayList<>();
for (int charPosition = 0; charPosition < input.length(); charPosition++) {
char currentChar = input.charAt(charPosition);
if(isVowel(currentChar)) {
vowelPositions.add(charPosition);
vowels.add(currentChar);
}
}
Collections.sort(vowels);
//now just iterate over all positions where we saw a vowel, and populate it with the vowels from the sorted list
for(Integer vowelPosition : vowelPositions) {
char vowel = vowels.remove(0);
result.setCharAt(vowelPosition, vowel);
}
return result.toString();
}
https://stackoverflow.com/questions/56350177
复制相似问题