The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java Program to count number of vowels in a string

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Java Program to count number of vowels in a string Posted: Sep 13, 2015 10:01 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Java Program to count number of vowels in a string
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement

Solution #1:

  1. package com.javatutorial;
  2.  
  3. public class CountNumberofVowels {
  4.  
  5.  public static void main(String[] args) {
  6.   
  7. System.out.println("Please enter a String");
  8. Scanner in = new Scanner(System.in);                  
  9.  String input = in.nextLine();
  10.  
  11.      char[] Chararray = input.toCharArray();
  12.  
  13.  int count = 0;
  14.  
  15.  for (char ch : Chararray) {
  16.       switch (ch) {
  17.              case 'a':
  18.              case 'e':
  19.              case 'i':
  20.              case 'o':
  21.              case 'u':
  22.              case 'A':
  23.              case 'E':
  24.              case 'I':
  25.              case 'O':
  26.              case 'U':
  27.                  count++;
  28.      break;
  29.      default:
  30.             
  31.      }
  32.  }
  33.        System.out.println("Number of vowels in String "+count);
  34.  
  35. }

  36. }
Output:

  1. Please enter a String
  2. Instance Of Java
  3. Number of vowels in String 6
     
Solution #2:

  1. package com.javatutorial;
  2.  
  3. public class CountNumberofVowels {
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.  int vowels = 0, digits = 0, blanks = 0; char ch; 

  8. System.out.println("Please enter a String");
  9. Scanner in = new Scanner(System.in);                  
  10. String testString= in.nextLine();
  11.  
  12.  
  13. for(int i = 0; i < testString.length(); i ++)
  14. {
  15.  
  16.   ch = testString.charAt(i);
  17.  
  18.   if(ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' ||   
  19.       ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
  20.           vowels ++;
  21.        else if(Character.isDigit(ch))
  22.           digits ++;
  23.      else if(Character.isWhitespace(ch))
  24.            blanks ++;
  25.   }
  26.  
  27.     System.out.println("Vowels : " + vowels);
  28.     System.out.println("Digits : " + digits);
  29.     System.out.println("Blanks : " + blanks);           
  30.    
  31.  
  32. }

  33. }
Output:

  1. Please enter a String
  2. vowels and consonants
  3. Vowels : 6
  4. Digits : 0
  5. Blanks : 2

Read: Java Program to count number of vowels in a string

Topic: The big greenfield cannabis cloud Previous Topic   Next Topic Topic: Rest client calls with Spring Cloud

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use