Hello guys, if you are wondering how to count number of vowels and consonants in a given word in Java then you have come to the right place. In the past, I have shared 100+ data structure questions and more than 75 programming interview questions, and In this article, we will take on a popular programming exercise of counting vowels in a word. You need to write a Java program to count how many vowels in a String, which is entered from the command prompt. It's similar to the program of counting the occurrence of characters in a String, in fact, it's a special case, where you need to count occurrences of all vowels, which includes five characters a, e, i, o and u. We will also use Scanner to get input from the user to make this this program an interactive exercise as shown in this article. Though I have put down all code inside the main method for quick testing. If you are asked to write this program as part of your homework or during the interview, better writing a method called public int countVowels(String word) and put the logic of counting vowels there. That's a better coding style than writing everything inside the main method. By the way, you can also use this logic to count number of consonants in a Java String. What you need to do is first count number of vowels and then subtract those characters from length of String, but remember this will only work if your String contains only alphabetic words, if it contains special character like @, _, | or numbers like 1,2,3 etc, than it will not work. In that case you need to extend your logic to only count consonants, by extending your switch case to include rest of 21 characters from English alphabets. Java Program to count vowels and consonants in String Here is our complete Java Program to count the number of vowel and consonants in Java: import java.util.Scanner; /** * Java Program to count vowels in a String. It accept a String from command prompt * and count how many vowels it contains. To revise, 5 letters a, e, i, o and u are * known as vowels in English. */ public class VowelCounter { public static void main(String args[]) { System.out.println("Please enter some text"); Scanner reader = new Scanner(System.in); String input = reader.nextLine(); char[] letters = input.toCharArray(); int count = 0; for (char c : letters) { switch (c) { case 'a': case 'e': case 'i': case 'o': case 'u': count++; break; default: // no count increment } } System.out.println("Number of vowels in String [" + input + "] is : " + count); } } Output: Please enter some text How many vowels in this String Number of vowels in String [How many vowels in this String] is : 7 You can see that the above String contains 7 vowel characters, highlighted by red font. This method is pretty quick as we are only accessing the array and the using switch to compare it to another character. If you notice the switch statement that you will see that we are using fall-through approach, means there is no break for all five cases, as you don't need to put break after every case. it will only increase count at one time, because increment is done at last case statement. By the way, you can reduce all this code to just few lines using Java 8 Lambda and Stream API as shown below: Arrays.stream(input.split("")) .filter(character -> "AEIOU".contains(character.toUpperCase())) .count(); That's all folks on this programming exercise to count Vowel and consonants in Java. It's interesting to count number of vowels or consonants in Java String, and you can do it differently as well. If you are keen to do more programming exercise, try to come out with a different solution and then compare that with this solution. Other Java Programming exercise for Practice How to check Prime numbers in Java Write a Java program to calculate GCD of two numbers How to print pyramid pattern in Java How to Find Nth Fibonacci Number in Java? How to rotate an array in Java? How to transpose a Matrix in Java? How to print a left triangle star pattern in Java How to implement Fibonacci series with caching Write a Java program to check Armstrong number in Java How to find if a number is palindrome in Java Java Program to print Fibonacci Series using Recursion Thanks for reading this article so far. If you find this question interesting and my solution worth learning then big thank you. Feel free to ask any question or doubt you have in comment section.
JAVA BABE COFFEE I’m married to this stuff, but I guess I’m willing to share the love (weird marriage dynamic??) If you’re my internet friend this code is for you to get a little $$ off a bag of the...
Explore Picturejourneys' 10537 photos on Flickr!
Visit the post for more.
A Modern Approach to Functional Programming Objects First with Java: A Practical Introduction is an introduction to object-oriented programming for beginners. The main focus of the book is general object-oriented and programming concepts from a software engineering perspective. The first chapters are written for students with no programming experience with later chapters being more suitable for advanced or professional programmers. The Java programming language and BlueJ-the Java development environment - are the two tools used throughout the book. BlueJ's clear visualisation of classes and objects means that students can immediately appreciate the differences between them and gain a much better understanding of the nature of an object than they would from simply reading source code. Unlike traditional textbooks, the chapters are not ordered by language features but by software development concepts. The Sixth Edition goes beyond just adding the new language constructs of Java 8. The book's exploration of this new language demonstrates a renaissance of functional ideas in modern programming. While functional programming isn't new in principle, it's seen a boost in popularity based on the current computer hardware available and the changing nature of projects programmers wish to tackle. Functional language constructs make it possible to efficiently automate currency, make use of multiple cores without much effort on the side of the programmer, are both more elegant and readable, and offer great potential in solving the issue of parallel hardware. Functional programming has become an essential part of the field, and Objects First with Java gives students a basic understanding of an area they'll need to master in order to succeed in the future.
This is the "partner" of the other JS. They both look more or less the same, so how can we tell which one is male or female? Well, the female is always looking at her reflection off the water's edge, hehehe.
In this article, we'll dive into the world of coffee podcast names that will surely perk up your day.
The Java Array Fill Method assigns user-specified values to each element in a specified range of an array. The syntax is Java Arrays.fill().
I Love Java themed coding badge. Java is an object-oriented programming language developed by Sun Microsystems in the early 1990s. Geek, programmer, coder, computer, gamer 25 mm button badge nerd fun
Gelatik Jawa (Padda Oryzivora)adalah jenis burung kicau berukuran kecil dengan panjang lebih kurang dari 15 cm dari suku Estrildidae .Burung ini memiliki kepala hitam ,pipi putih dan paruh merah yang berukuran besar dan kuat.Burung dewasa berwarna abu-abu,perut yang berwarna coklat kemerahan,kaki merah muda dan terdapat lingkaran merah disekitar matanya.Burung jantan dan betina serupa tetapi burung muda berwarna coklat. Burung ini endemik dari indonesia dan di alam banyak ditemukan di hutan,padang rumput,sawah dan lahan budidaya di Pulau Jawa dan Bali.sekarang burung ini dikenali dibanyak negara diseluruh dunia sebagai burung hias. Perilakunya senang berkelompok dan cepat berpindah-pindah,makannan utamanya adalah bulir padi atau beras juga biji-bijian lain ,buah-buahan dan serangga .Burung betina menetaskan antara 4 sampai 6 butir telur yang berwarna putih. Spesies ini merupakan salah satu burung yang paling diminati oleh para pemelihara burung.penangkapan liar,hilangnya habitat hutan serta terbatasnya ruang hidup burung ini sebagai salah satu faktor penyebab menyusutnya populasi burung gelatik dan bahkan terancam punah di habitat aslinya dalam waktu singkat. Sehingga sekarang jarang sekali melihat populasi burung ini disekitar kita. Artikel Terkait Burung
WEIGHT SLIMMING EXPERT: 15-Second Fat Burning Ritual Drink This Coffee Every Morning WATCH NOW Have you struggled to lose belly fat? Need more energy? It is the world’s first and only natural…
Men ride horseback at the base of Indonesia’s Mount Bromo in this National Geographic Photo of the Day.
If you have a vintage inspired kitchen or would like a touch of vintage to your decor, you don't have to go searching through thrift shop or auctions...
I'm done.