Home / Programs / Vowel Count and Consonant Count using Java
🚀 Programming Example

Vowel Count and Consonant Count using Java

👁 129 Views
💻 Practical Program
📘 Step Learning

Vowel Count and Consonant Count using Java

📌 Information & Algorithm

Given Input:

Enter a String: Hello

Expected Output:


Here is your vowel count: 2
Here is your consonant count: 3


💻 Program Code

import java.util.*;
class VowelCount{
	public static void main(String args[]){
		String userString;
		int loopVariable, vowelCount = 0, consonantCount = 0, lengthOfString;
		Scanner sc =  new Scanner(System.in);
		System.out.print("Enter a String: ");
		userString = sc.nextLine();

		lengthOfString = userString.length();

		for(loopVariable = 0; loopVariable < lengthOfString; loopVariable++)
		{
			char presentCharacter = userString.charAt(loopVariable);
			if( presentCharacter == 'a' || presentCharacter == 'e' || presentCharacter == 'i' ||
			presentCharacter == 'o' || presentCharacter == 'u' || presentCharacter == 'A' || presentCharacter == 'E'||
			presentCharacter == 'I' || presentCharacter == 'O' || presentCharacter == 'U'){
				vowelCount = vowelCount + 1;
			}else{
				consonantCount = consonantCount + 1;
			}
		}
		System.out.println("Here is your vowel count: "+vowelCount);
		System.out.println("Here is your consonant count: "+consonantCount);
	}
}
                        

🖥 Program Output

Enter a String: Hello
Here is your vowel count: 2
Here is your consonant count: 3
Press any key to continue . . .

                            

📘 Explanation


import java.util.*;
class VowelCount{
	public static void main(String args[]){
		String userString;
		int loopVariable,  vowelCount = 0, consonantCount = 0,  lengthOfString;
		Scanner sc =  new Scanner(System.in);
		System.out.print("Enter a String: ");
		userString = sc.nextLine();

		lengthOfString = userString.length();

		for(loopVariable = 0; loopVariable < lengthOfString; loopVariable++)
		{
			char presentCharacter = userString.charAt(loopVariable);
			if( (presentCharacter >= 'A'  && presentCharacter <= 'Z') || (presentCharacter >= 'a'  && presentCharacter <= 'z'))
			{
				if( presentCharacter == 'a' || presentCharacter == 'e' || presentCharacter == 'i' ||
				presentCharacter == 'o' || presentCharacter == 'u' || presentCharacter == 'A' || presentCharacter == 'E'||
				presentCharacter == 'I' || presentCharacter == 'O' || presentCharacter == 'U')
				{
					vowelCount = vowelCount + 1;
				}
				else
				{
					consonantCount = consonantCount + 1;
				}

			}

		}
		System.out.println("here is your length Of String: "+lengthOfString);
		System.out.println("Here is your vowel count: "+vowelCount);
		System.out.println("Here is your consonant count: "+consonantCount);
	}
}
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.