句子的元音按降序排列

时间:2019-05-28 21:17:23

标签: java

假设我输入以下字符串:

" Hello party"

您如何仅以降序方式订购该链的元音?

例如:

"Halle porty"

我已经尝试通过以下方式解决问题,但是当我运行它时,它告诉我我有一个ExeptionBounce5。我该如何纠正它?我还被告知负责人如下:

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;
        }

1 个答案:

答案 0 :(得分:0)

您的代码甚至无法编译-您如何运行它?

要回答您的问题,以下是一段代码,给定一个字符串,该字符串将返回带有已排序的元音的字符串:

    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();
    }