Tuesday, November 19, 2013

How to count Vowels and Consonants in Java String Word

Java Program to count Vowels and Consonants in a word using JavaIn 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 command prompt. It's similar to program of counting 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 further use Scanner to get input from user, as shown in this article. Though I have put down all code inside main method for quick testing, if you are asked to write this program as part of your homework or during interview, better writing a method called public int countVowels(String word) and put logic of counting vowels there. That's a better coding style than writing everything inside 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

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':
                  // no count increment

            System.out.println("Number of vowels in String [" + input + "] is : " + count);


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 above String contains 7 vowel characters, highlighted by red font. This method is pretty quick as we are only accessing array and the using switch to compare it to another character. If you notice 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.

That's all folks on this programming exercise, 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
Java Program to print Fibonacci Series using Recursion
Write a Java program to check Armstrong number in Java
How to find if a number is palindrome in Java
How to check Prime numbers in Java
Write a Java program to calculate GCD of two numbers

No comments:

Post a Comment

Java67 Headline Animator