- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to count all vowels in a string
Let’s say the following is our string.
String str = "!Demo Text!";
To count vowels, loop through each every character and check for any of the vowel i.e.
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { ++vowelsCount; }
The variable “vowelsCount” will give the count of all vowels.
Example
public class Demo { public static void main(String[] args) { String str = "!Demo Text!"; int vowelsCount = 0; for(char c : str.toCharArray()) { c = Character.toLowerCase(c); if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { ++vowelsCount; } } System.out.println("String "+str+" has "+ vowelsCount + " vowels."); } }
Output
String !Demo Text! has 3 vowels.
- Related Articles
- Java Program to count vowels in a string
- C# Program to count vowels in a string
- C++ Program to count Vowels in a string using Pointer?
- To count Vowels in a string using Pointer in C++ Program
- C# Program to count number of Vowels and Consonants in a string
- Python program to count number of vowels using set in a given string
- C# program to check for a string that contains all vowels
- Java program to count the number of vowels in a given sentence
- Python program to count the number of vowels using set in a given string
- Python program to count the number of vowels using sets in a given string
- Program to match vowels in a string using regular expression in Java
- Java Program to Count the Number of Vowels and Consonants in a Sentence
- Count and display vowels in a string in Python
- Replace All Consonants with Nearest Vowels In a String using C++ program
- C Program to count vowels, digits, spaces, consonants using the string concepts

Advertisements