consonant

Count total number of vowels, consonants and space of a sentence in Java

Count number of vowels, consonants and spaces in a sentence in Java

In this article, we will discuss the concept of Count total number of vowels, consonants and spaces in a sentence in Java

In this post, we are going to learn how to write a program count the total number of Vowels , consonants and spaces in the given sentence entered by user using loops in Java language

Code to count total number of Vowel, consonant and spaces

Code to count number of Vowel, consonant and spaces in given sentence, using for loop   – #1

In this code, we are going to learn how to count  the total number of  Vowels, consonants and spaces in the given sentence using for loop in Java language

Program 1

import java.util.Scanner;
public class CountVowelAndConsonantspace{
public static void main(String args[]){
  String str;
int vowCount=0,consCount=0,spaces=0;

 Scanner scan=new Scanner(System.in); 
  //create a scanner object for input
  
System.out.println("Enter the Sentence for count vowel and cons and spaces  ");
str=scan.nextLine();
int i=0; 
for(i=0; i<str.length(); 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' ){
    vowCount++;
  }
  else if((ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' )){
    consCount++;
  }
  else if(ch ==' '){
spaces++;  
  }
}
System.out.println("Number of vowels: "+vowCount);
System.out.println("Number of consonant: "+consCount);
System.out.println("Number of spaces: "+spaces);
}
}

When the above code is executed, it produces the following result

Enter the Sentence for count vowel and cons and spaces
This is my Java code
Number of vowels: 6
Number of consonant: 10
Number of spaces: 4

 

Code to count number of Vowel, consonant and spaces in given sentence, using while loop   – #2

In this code, we are going to learn how to count  the total number of  Vowels, consonants ans spaces in the given sentence using while loop in Java language

Program 2

import java.util.Scanner;
public class CountVowelAndConsonantspacewhile{
public static void main(String args[]){
  String str;
int vowCount=0,consCount=0,spaces=0;

 Scanner scan=new Scanner(System.in); 
  //create a scanner object for input
  
System.out.println("Enter the Sentence for count vowel and cons and spaces  ");
str=scan.nextLine();
int i=0; 
while(i<str.length()){
  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' ){
    vowCount++;
  }
  else if((ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' )){
    consCount++;
  }
  else if(ch ==' '){
spaces++;  
  }
   i++;
}
System.out.println("Number of vowels: "+vowCount);
System.out.println("Number of consonant: "+consCount);
System.out.println("Number of spaces: "+spaces);
}
}

When the above code is executed, it produces the following result

Enter the Sentence for count vowel and cons and spaces
Java is best for coders
Number of vowels: 7
Number of consonant: 12
Number of spaces: 4

 

Code to count number of Vowel, consonant and spaces in given sentence, using do-while loop   – #3

In this code, we are going to learn how to count  the number of  Vowel, consonant and spaces in the given sentence using do-while loop in Java language

Program 3

import java.util.Scanner;
public class CountVowelAndConsonantspacewhile{
public static void main(String args[]){
  String str;
int vowCount=0,consCount=0,spaces=0;

 Scanner scan=new Scanner(System.in); 
  //create a scanner object for input
  
System.out.println("Enter the Sentence for count vowel and cons and spaces  ");
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' ){
    vowCount++;
  }
  else if((ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' )){
    consCount++;
  }
  else if(ch ==' '){
spaces++;  
  }
   i++;
}while(i<str.length());
System.out.println("Number of vowels: "+vowCount);
System.out.println("Number of consonant: "+consCount);
System.out.println("Number of spaces: "+spaces);
}
}

When the above code is executed, it produces the following result

Enter the Sentence for count vowel and cons and spaces
Java easy language for beginners
Number of vowels: 12
Number of consonant: 16
Number of spaces: 4

 

Suggested post

if statement in Java language

Nested if statements in Java language

Method in Java language

For loop in Java language

While loop in Java language

Do-while loop in Java language

Operator in Java language

Variable in Java language

Data type in Java language

class and main method in Java

 

Similar post

Java program to print all alphabets in given range

C program to print all alphabets in given range

C++ program to print all alphabets in given range

 

Java program to print all alphabets using loops

C program to print all alphabets using loops

C++ program to print all alphabets using loops

 

Java program to check whether a character is Alphabet or Not

C++ program to check whether a character is Alphabet or Not

C program to check whether a character is Alphabet or Not

Python program to check whether a character is Alphabet or Not

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.