0
    import java.util.*;
    class VowelAsc
    {
        public static void main(String args[])
        {
            int count=0;
            Scanner sc=new Scanner(System.in);
            int n=sc.nextInt();
            String [] s=new String[n];
            int [] b=new int[40];
            for(int i=0;i<n;i++)
            { 
                s[i]=sc.next();
            }
            for(int i=0;i<s.length;i++)
            {
                char[] a=s[i].toCharArray();
                for(int c=0;c<a.length;c++)
                {

                    if(a[c]=='a' || a[c]=='e' || a[c]=='i' || a[c]=='o' || a[c]=='u' ||a[c]=='A' ||a[c]=='E' || a[c]=='I' || a[c]=='O' || a[c]=='U')
                    {
                       count++;
                       //b[c]=count;
                    }
                }
                if(count>0)
                {
                    if(i<s.length)
                    {
                        String t=s[i];
                        s[i]=s[i+1];
                        s[i+1]=t;
                    }
                } 
           }
      }
 }

I am trying to count the vowels present in each string and i wanted to swap the strings based on count variable which i am unable to do. After accepting the strings i am converting it into char array with toCharArray() function and comparing each character with lower and upper case vowels.

I am getting an error. Any help in writing the part of the code would be appreciated.

Input:

n=4
xyz
bad
aeiou
hello

Output:

aeiou
hello
bad
xyz
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
mahesh
  • 1
  • 7

2 Answers2

3

Well... this might be slightly over the top (List and RegEx), but if you don't have to execute this millions of times, then sorting them in a list via a custom Comparator will do the job:

String[] s = new String[]{"xyz", "bad", "aeiou", "hello"};

Arrays.sort(s, new Comparator<String>(){
    @Override
    public int compare(String o1, String o2) {
        return o2.replaceAll("[^aeiouAEIOU]", "").length()
                - o1.replaceAll("[^aeiouAEIOU]", "").length();
    }
});

EDIT: Optimized by removing the List, thanks @Holger

bkis
  • 2,530
  • 1
  • 17
  • 31
  • could you please explain about String o1 and o2. – mahesh Jan 10 '17 at 14:41
  • @mahesh they are the methods parameter as `compare(String o1, String o2)` – SomeJavaGuy Jan 10 '17 at 14:43
  • He is using a comparator function, which effectively goes through all instances in a collection of values, and figures out which of the two have the lowest/highest value. Try googling bubble sort, and you will get an idea of how it works. Although this is a slightly advanced answer, it is a very good one. – jumps4fun Jan 10 '17 at 14:44
  • 1
    @mahesh the compare()-method of the Comparator decides whether the first or second given String (o1 or o2) is "greater" in the sort order. In this case, both Strings are reduced to only their vowels and then their length is compared. compare() gives a negative int if o1 is "less" then o2, so we just subtract the length of o1 from the length of o2. – bkis Jan 10 '17 at 14:44
  • There is no need for the final `sList.toArray(s);` call. You already sorted the array, as `Arrays.asList(s)` returned a wrapper around the array. Every modification of the `sList` modifies the underlying `s` array. Of course, when you just use `Arrays.sort(s, your comparator)`, you don’t even need `Arrays.asList`… – Holger Jan 10 '17 at 17:59
  • @Holger Thanks, I forgot i could just sort the array directly, as i'm used to using the Collection framework. You're right, it doesn't make sense to use the List then. – bkis Jan 11 '17 at 10:57
1

Similar to mumpitz code, but optimized for performance:

class Comp implements Comparator<String> {
  private static boolean[] isVowel = new boolean[127];
  static {
    isVowel['a'] = true;
    isVowel['e'] = true;
    isVowel['i'] = true;
    isVowel['o'] = true;
    isVowel['u'] = true;
  }
  @Override

  public int compare(String o1, String o2) {
    return count(o1) - count(o2); 
  }

  private int count(String s) {
    int cnt = 0;
    for (int i=0; i<s.length(); i++) {
      char c = s.charAt(i);
      if (c < 128 && isVowel(c))
        cnt++;
      }
    }
    return cnt;
  }
}

String[] s = new String[]{"xyz", "bad", "aeiou", "hello"};
List<String> sList = Arrays.asList(s);

Collections.sort(sList, new Comp());
s = sList.toArray(s);
Roberto Attias
  • 1,883
  • 1
  • 11
  • 21
  • “Similar to mumpitz code, ” the call `sList.toArray(s);` is obsolete. See [this comment](http://stackoverflow.com/questions/41571190/how-can-i-sort-a-string-array-in-ascending-order-by-number-of-vowels-in-each-st#comment70355431_41571425). – Holger Jan 10 '17 at 18:02