Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
C++ program to count number of vowels, consonants in a string

Java program to count vowels and consonants in a string

Posted on April 19, 2022April 19, 2022

Table of Contents

  • Java program to count vowels and consonants in a string
    • Code to count Vowels or consonants in a string
      • Code to count number of Vowel and consonant in a string, using for loop   – #1
      • Code to count number of Vowel and consonant in a string, using while loop   – #2
      • Code to count number of Vowel and consonant in a string, using do-while loop   – #3

Java program to count vowels and consonants in a string

In this article, we will discuss the concept of Java program to count vowels and consonants in a string

In this post, we are going to learn how to write a program to  count total number of vowels and consonants in given string input from user in Java language

Code to count Vowels or consonants in a string

Code to count number of Vowel and consonant in a string, using for loop   – #1

In this code, we are going to learn how to count  the total number of  Vowel and consonant in the given String using for loop in Java language

Program 1

import java.util.Scanner;
public class CountVowelsAndConsonants{
public static void main(String args[]){
  String str;//declare a string
int vowCount=0,consCount=0;

 Scanner scan=new Scanner(System.in); 
  //create a scanner object for input
  
System.out.println("Enter the String for count vowel and consonant  ");
//Ask input from uer as string
str=scan.nextLine();//reading the input
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++;
  }
}
System.out.println("Number of vowels: "+vowCount);
//display number of vowels
System.out.println("Number of consonant: "+consCount);
//display number of consonants
}
}

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

Enter the String for count vowel and consonant
My Java
Number of Vowels: 2
Number of consonant: 4

 

Code to count number of Vowel and consonant in a string, using while loop   – #2

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

Program 2

import java.util.Scanner;
public class CountVowelsAndConsonants1{
public static void main(String args[]){
  String str;//declare character variable
int vowCount=0,consCount=0;
//initiolize variables vowCount and consCount as zero
 Scanner scan=new Scanner(System.in); 
  //create a scanner object for input
  
System.out.println("Enter the String for count vowel and consonant  ");
//Ask input from user as string
str=scan.nextLine();//reading the string input
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++;
  }
   i++;
}
System.out.println("Num ber of vowels: "+vowCount);
//display number of vowels in the given string
System.out.println("Num ber of consonant: "+consCount);
//display number of consonant in the given string
}
}

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

Enter the String for count vowel and consonant
Programming
Number of Vowels: 3
Number of consonant: 8

 

Code to count number of Vowel and consonant in a string, using do-while loop   – #3

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

Program 3

import java.util.Scanner;
public class CountVowelsAndConsonantsAscii{
public static void main(String args[]){
  String str;//declare a string variable
int vowCount=0,consCount=0;
//initialize counter variable
 Scanner scan=new Scanner(System.in); 
  //create a scanner object for input
  
System.out.println("Enter the String for count vowel and consonant  ");
str=scan.nextLine();
int i=0; 
for(i=0; i<str.length(); i++){
  char ch=str.charAt(i);
  if(ch == 97|| ch == 101|| ch == 105|| ch == 111|| ch == 117
  ||ch == 65|| ch == 69|| ch == 73|| ch == 79|| ch == 85 ){
    vowCount++;
  }
  else if((ch >= 97 && ch <= 122 || ch >= 65 && ch <= 90 )){
    consCount++;
  }
}
System.out.println("The number of vowels: "+vowCount);
System.out.println("The number of consonant: "+consCount);
}
}

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

Enter the String for count vowel and consonant
Vowels and consonants
The number of vowels: 6
The number of consonants: 13

 

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

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes