Tuesday, 29 August 2017

Java Core | Java program to accept a line and check how many consonants and vowels are there in line

Java program to accept a line and check how many consonants and vowels are there in line

Program

import java.util.Scanner;
class consonants_vowels
{
          public static void main(String args[])
          {       
                   Scanner in = new Scanner(System.in);
                   String Str;
                   int i,vowels=0,consonants=0,Digit=0,whitespace=0;
                   char ch;
                   System.out.println("Enter a any String");
                   Str = in.nextLine();
                  
                   for(i=0;i<Str.length();i++)
                   {
                             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'))
                                {
                                         vowels++;
                                }
                             else if(Character.isDigit(ch))
                                {
                                         Digit++;
                                }
                             else if(Character.isWhitespace(ch))
                                {
                                         whitespace++;
                                }
                            
                             else
                                      {
                                                consonants++;
                                      }
                   }
                   System.out.println("in a line "+ vowels +" vowels");
                   System.out.println("in a line "+ consonants +" consonants");
                   System.out.println("in a line "+ Digit +" Digit");
                   System.out.println("in a line "+ whitespace +" whitespace");

          }

Output

how many consonants and vowels are there in line java core

0 comments:

Post a Comment