HI!!!I am new in java i would like to know sample program that counts the number of vowels of word

Dani AI

Generated

Hi — welcome to Java. 's outline points you in the right direction. Before you write code, decide whether you want to count only basic ASCII vowels (a,e,i,o,u), treat uppercase the same, include y as a vowel, or handle accented letters (like á, é). Those choices affect the simplest correct solution.

Here is a compact, safe Java approach that counts the five basic vowels case‑insensitively and uses code points so surrogate pairs (emoji, rare characters) do not break the loop:

public static int countVowels(String s) {
    if (s == null || s.isEmpty()) return 0;
    return (int) s.toLowerCase(java.util.Locale.ENGLISH)
                  .codePoints()
                  .filter(cp -> cp == 'a' || cp == 'e' || cp == 'i' || cp == 'o' || cp == 'u')
                  .count();
}

This runs in O(n) time and O(1) extra space. If you need accented vowels, normalize to NFD and strip diacritics with java.text.Normalizer, or add those accented code points to the filter. If you want y treated as a vowel, include it in the condition. Test edge cases: null, empty string, all-uppercase input, words with accents, and sentences with punctuation. If you get stuck implementing input reading or normalization, post what you tried and include the exact input so we can help refine it.

Recommended Answers

All 2 Replies

Well, if you meet one, introduce it to me, too.

HI!!!I am new in java i would like to know sample program that counts the number of vowels of word

what exactly do you want? to count the vowels in a 'word', as a String object, or in a 'Word' document :P
be a bit more specific.

I believe it is the first, so I'll give you a starting point, as in some sort of pseudo-code

Read a word
Set counter to 0
Perform for each letter (character) of that word
-- if the letter (character) is a vowel:
-- -- add 1 to counter
print: "there are " counter " vowels in the word"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.