Home Java Java-tips Data Strings 96string_examples Java: Example - Count vowels

Ask Questions?

View Latest Questions

Advertisement


 
 

Java: Example - Count vowels
Posted on: February 18, 2008 at 12:00 AM
This example method counts all vowels (a, e, i, o, u) in a string.

Java: Example - Count vowels

This example method counts all vowels (a, e, i, o, u) in a string.
//-------------------------------------------- countVowels()

int countVowels(String text) {
    int count = 0; // start the count at zero
    // change the string to lowercase
    text = text.toLowerCase();
  
    for (int i = 0; i < text.length(); i++) {
        char c = text.charAt(i);
        if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u') {
            count++;
        }
    }
    return count;
}
Copyleft 2003 Fred Swartz MIT License, Last update: 2003-10-29
Advertisement


DMCA.com