Drop Down

Tuesday, February 5, 2019

Program to count vowels, consonant, digits and special characters in string

package test;

import java.util.Scanner;

public class Count_vowel_cons_digit_symbol
{

public static void main(String[] args)
{
  System.out.println("Enter string");
  Scanner kb = new Scanner(System.in);
  String string = kb.nextLine();
  displaycount(string);
}

private static void displaycount(String s)
{
int vowel =0;
int consonent =0;
int whitespace = 0;
int digit = 0;
int symbol = 0;
String string = s.toLowerCase();
System.out.println(string);
   for(int i=0;i<string.length();i++)
   {
   char ch = string.charAt(i);
 
   if((ch>='a' && ch<='z')||(ch>='A' && ch<='Z'))
   {
   switch(ch)
   {
   case 'a':
   case 'e':
   case 'i':
   case 'o':
   case 'u':  
   vowel++;
   break;
   default:
   consonent++;
   }  
 
   }
   else if(ch>='0' && ch<='9')
        digit++;
   else if(Character.isWhitespace(ch))
        whitespace++;
   else
   {
   symbol++;
   }
   }
 
   System.out.println("Vowels: "+vowel);
   System.out.println("consonent: "+consonent);
   System.out.println("whitespace: "+whitespace);
   System.out.println("digit: "+digit);
   System.out.println("symbol: "+symbol);

}

}

1 comment:

  1. Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post. read

    ReplyDelete

Java 8 Notes Pics