Home > Java, Uncategorized > Counting vowels in Strings… the fast way…

Counting vowels in Strings… the fast way…

Came across an interesting little search in the dashboard of this blog the other day where someone was looking to count the number of vowels in a given String, or potentially an array of Strings. Doing this by direct String manipulation or regexping the values out could be a very time-consuming operation so I ran up a little piece of code to facilitate this (I can see some very practical applications for it for my own ends, so operating here from a position of enlightened self-interest!).

Anyway it’s straightforward enough to do via the StringBuffer:

public int vowelCounter(String s)
{
StringBuffer sb = new StringBuffer(s);
int retval = 0;
   for (int i = 0; i < sb.length(); i++)
    {
        char cTest = sb.charAt(i);
                
        
        switch (cTest) {
            case 'a':  retval++; break;
            case 'e':  retval++; break;
            case 'i': retval++; break;
            case 'o':  retval++; break;
            case 'u':  retval++; break;
            case 'A':  retval++; break;
            case 'E':  retval++; break;
            case 'I': retval++; break;
            case 'O':  retval++; break;
            case 'U':  retval++; break;
            default: ;
        }
       
    }
return retval;
 }

Obviously if it’s more than just one String, such as an array of Strings or other Collection type, you just have to have a counter variable, then iterate through the array or other collection type calling the vowelCounter method with the String to be dealt with at the time, incrementing the counter variable as necessary on each loop.

We can make this problem more interesting by extending it a little and asking ‘what if we need to know where the vowels are?’ Well, if you’ve looked at the post on the subject of cleaning whitespace, there’s a nice little StringBuffer prototype there for this already in the identifyWS method, which I’ll repeat here to save you scrolling down the page.

 private int[] identifyWS(StringBuffer sb)
    {
    List l = new ArrayList();
    for (int i = 0; i < sb.length(); i++)
    {
        if (sb.charAt(i) == ' ')
        {
        l.add(i);
        }
    }
    return at.toIntArray(l);
    }

The modfication required here to make this work is trivial, and really all we need to do is rename the method and substitute a slightly modfied version of the switch statement in vowelCount for the if condition:

 private int identifyVowels(StringBuffer sb)
    {
    List l = new ArrayList();
    for (int i = 0; i < sb.length(); i++)
    {
        char cTest = sb.charAt(i);
               
        
        switch (cTest) {
            case 'a':   l.add(i); break;
            case 'e':  l.add(i); break;            
            case 'i':  l.add(i); break;
            case 'o':   l.add(i); break;
            case 'u':   l.add(i); break;
            case 'A':   l.add(i); break;
            case 'E':   l.add(i); break;
            case 'I':  l.add(i); break;
            case 'O':   l.add(i); break;
            case 'U':   l.add(i); break;
            default: break;
        }
    }
    return toIntArray(l);
    }

And you’ll need the toIntArray method, also given earlier:


public int[] toIntArray(List<Integer> integerList) {
int[] intArray = new int[integerList.size()];
for (int i = 0; i < integerList.size(); i++) {
intArray[i] = integerList.get(i);
}
return intArray;
}

This will return you an array of positions of all the vowels in the passed String.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: