In this article, we will discuss the concept of the program to count vowels, consonants, words, characters and space in Java
In this post, we are going to learn how to count the total number of Vowels, consonants, words, character and Space of the given string in Java programming language
The program allows the user to enter a String and then it counts and display the total number of vowels, consonants, words, character and Space of the given string using for loop in Java programming language
program 1
//program to count vowel,consonant,words, character and space import java.util.Scanner; public class CountVowelWordsCharactersSpace{ public static void main(String args[]){ //variable declaration String str; int words=1,characters=1; int vCount=0,cCount=0; //vriable declaration and initialization Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.println("Enter the String "); str=scan.nextLine(); for(int i=0; i<=str.length()-1; i++){ char ch=str.charAt(i); if(ch == 'a'|| ch == 'e'|| ch == 'i'|| ch == 'o'|| ch == 'u' ||ch == 'A'|| ch == 'E'|| ch == 'I'|| ch == 'O'|| ch == 'U' ) { vCount++; } else if((ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' )){ cCount++; } if(str.charAt(i)==' ' && str.charAt(i+1)!=' '){ words++; } else if(str.charAt(i)!=' '){ characters++; } } System.out.println("\nTotal Vowels: "+vCount); System.out.println("Total consonants: "+cCount); System.out.println("\nTotal words: "+words); System.out.println("Total characters: "+characters); System.out.println("Space: "+(words-1)); } }
When the above code is executed, it produces the following result
Enter the String
this is java
Total Vowels: 4
Total consonants: 6
Total words: 3
Total characters: 11
Space: 2
The program allows the user to enter a String and then it counts and display the total number of vowels, consonants, words, character and Space of the given string using While loop in Java programming language
program 2
//program to count vowel,consonant,words, character and space import java.util.Scanner; public class CountVowelWordsCharactersSpace1{ public static void main(String args[]){ //variable declaration String str; int words=1,characters=1; int vCount=0,cCount=0; //vriable declaration and initialization Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.println("Enter the String "); str=scan.nextLine(); int i=0; while(i<=str.length()-1){ char ch=str.charAt(i); if(ch == 'a'|| ch == 'e'|| ch == 'i'|| ch == 'o'|| ch == 'u' ||ch == 'A'|| ch == 'E'|| ch == 'I'|| ch == 'O'|| ch == 'U' ){ vCount++; } else if((ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' )){ cCount++; } if(str.charAt(i)==' ' && str.charAt(i+1)!=' '){ words++; } else if(str.charAt(i)!=' '){ characters++; } i++; } System.out.println("\nTotal Vowels: "+vCount); System.out.println("Total consonants: "+cCount); System.out.println("\nTotal words: "+words); System.out.println("Total characters: "+characters); System.out.println("Space: "+(words-1)); } }
When the above code is executed, it produces the following result
Enter the string Java is the best language for beginners Total vowels: 13 Total consonants: 20 Total words: 7 Total characters: 33 Space: 6
The program allows the user to enter a String and then it counts and display the total number of vowels, consonants, words, character and Space of the given string using do-while loop in Java programming language
program 3
//program to count vowel,consonant,words, character and space import java.util.Scanner; public class CountVowelWordsCharactersSpace2{ public static void main(String args[]){ //variable declaration String str; int words=1,characters=0; int vCount=0,cCount=0; //vriable declaration and initialization Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.println("Enter the String "); str=scan.nextLine(); int i=0; do{ char ch=str.charAt(i); if(ch == 'a'|| ch == 'e'|| ch == 'i'|| ch == 'o'|| ch == 'u' ||ch == 'A'|| ch == 'E'|| ch == 'I'|| ch == 'O'|| ch == 'U' ){ vCount++; } else if((ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' )){ cCount++; } if(str.charAt(i)==' ' && str.charAt(i+1)!=' '){ words++; } else if(str.charAt(i)!=' '){ characters++; } i++; }while(i<=str.length()-1); System.out.println("\nTotal Vowels: "+vCount); System.out.println("Total consonants: "+cCount); System.out.println("\nTotal words: "+words); System.out.println("Total characters: "+characters); System.out.println("Total Spaces: "+(words-1)); } }
When the above code is executed, it produces the following result
Enter the string Java language Total vowels: 6 Total consonants: 6 Total words: 2 Total characters: 12 Total Spaces: 1
Suggested for you
do-while loop in Java language
Similar post
C++ program to count the total number of characters in the given string
C program to count the total number of characters in the given string
Python program to count the total number of characters in the given string
Java program to count the total number of upper case lower case numeric and special character
C program to count the total number of upper case lower case numeric and special character
Python program to count the total number of upper case lower case numeric and special character
C++ program to count the total number of upper case lower case numeric and special character
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…