Java Snippet to count number of vowels in a string

import java.util.*;
class CountVowel
{
public static void main(String s[])
{
Scanner sc = new Scanner(System.in);
int count = 0;
char ch;
System.out.println("Enter The String To Count No. Of Vowels : ");
String str = sc.nextLine();
for(int i=0;i<str.length();i++)
{
ch = str.charAt(i);
if((ch == 'a') || (ch == 'e') || (ch == 'i') || (ch == 'o') || (ch == 'u'))
{
count++;
}
}
System.out.println("The no of vowels in given string is : " + count);
}
}

Comments

  1. hey..would the code for this be any different if you were asked to write code to count number of vowels for a given file i.e you do not have to enter any text but its already provided?

    ReplyDelete

Post a Comment