loops

Java program to count vowels and consonants in a string

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

Karmehavannan

Recent Posts

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

11 months ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

11 months ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

11 months ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

11 months ago

Function/method to convert Celsius into Fahrenheit -Entered by user

Function/method to convert Celsius into Fahrenheit -Entered by user In this article, we will discuss…

11 months ago

Temperature conversion: Celsius into Fahrenheit using function or method

Temperature conversion: Celsius into Fahrenheit using a function or method In this article, we will…

11 months ago

This website uses cookies.