Program to Count vowels Consonant Used in the Sentence

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

  1. Start
  2. Maintain counter for vowel and Consonant
    • vowelcount=0,Constcount=0
  3. Take String inputs s=”String data”
  4. Convert Inputted String into Lowercase
    • s=s.toLowerCase
  5. 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++
  6. Print vowelcount++
  7. Print constcount++
  8. 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

Leave a Comment