In this Program we will take String as an Input and will find Number of vowels and Number Consonant letters in the Sentence.
Logic:
Just Compare each letter with vowels and if character = vowel increase vowelcount++ else increase constcount++
Algorithm/Program Explanation
- Start
- Maintain counter for vowel and Consonant
- vowelcount=0,Constcount=0
- Take String inputs s=”String data”
- Convert Inputted String into Lowercase
- s=s.toLowerCase
- Define and Initialize For loop
- for(int i=0;i<s.length();i++)
- Check for Condition
- if(s.charAt(i)==’a’|| s.charAt(i)==’e’|| s.charAt(i)==’i’|| s.charAt(i)==’o’|| s.charAt(i)==’u’)
- vowelcount++
- else if (s.charAt(i)>=’a’ && s.charAt(i)<=’z’)// to avoid white spaces to be calculated
- constcount++
- Print vowelcount++
- Print constcount++
- End
Example-1 Taking Static Inputs
import java.util.*;
public class countconstvowel{
public static void main(String args[]){
int vowelcount=0,constcount=0;
String s="Noteshacker is an amazing website ";
s=s.toLowerCase();
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='a'|| s.charAt(i)=='e'|| s.charAt(i)=='i'|| s.charAt(i)=='o'|| s.charAt(i)=='u'){
vowelcount++;
}
else if (s.charAt(i)>='a' && s.charAt(i)<='z'){
constcount++;
}
}
System.out.println("Number of Vowels in the Sentences is:"+ vowelcount);
System.out.println("Number of Consonant in the Sentences is:"+ constcount);
}
}
Output:
Number of Vowels in the Sentences is:12
Number of Consonant in the Sentences is:17
Questions asked in this Topic
- Write a Program to Count Numbers of vowels and Consonant in the Sentence?
- Write a Logic to count vowels and Consonant?
- Derive a code check if vowels are present in the sentence,if yes count number of vowel?
Related Topic
- Arithmetic Operations Java Program
- Java program to perform Logical and Relational operations
- Java program to Implement Mathematical functions defined in Java Math class
- Java basic Programs with Explanation
- Java Program to find Min,Max Element in an Array
- Java Program to sort Array Elements in ascending and descending order
- Java Program to display Array item at Even & Odd position
- Program to Calculate Sum and Average of Array elements
- Java Program to find Index of specific Element in an array
- Program to find common elements between two arrays
- Java Program to Add/Insert specific Element to an Array
- Java Program to delete specific element from an Array
- Program to calculate frequency of each element in Array
- Java Program to find Even and Odd Numbers in the arrays
- Java Program to search Given Number in an Array
- Java Program to Add or Sub Two Matrices
- Java Program to Multiply two or More Matrices
- Java Program to Print Array in Reverse Order
- Java Program To Transpose Given Matrix
- Java Programs List- Java Programs Examples With Output