Program to count vowels,consonants,words, characters and space in Java
- Home
- Characters
- Program to count vowels,consonants,words, characters and space in Java
- On
- By
- 0 Comment
- Categories: Characters, do-while, for loop, String, While loop
Program to count vowels,consonants,words, characters and space in Java
Program to count vowels, consonants, words, characters and space in Java
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
Java code to find Vowels, consonants, words characters and space of a string
Count vowels, consonants, words, characters and space using for loop
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
Count vowels, consonants, words, characters and space using while loop
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
Count vowels, consonants, words, characters and space using do-while loop
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