String

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

Count vowels,consonants,words, characters and spaces

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

for loop in Java language

while loop in Java language

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 Program to multiplication table using Array
C# Full Pyramid star pattern program
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

2 weeks ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

2 weeks ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

4 weeks ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

4 weeks ago

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

7 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

7 months ago