Codex

Count Vowels

Jul 27th, 2011
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.59 KB | None | 0 0
  1. import java.util.Scanner;
  2. class CountVowels
  3. {
  4. public static void main(String args[])
  5. {
  6. Scanner sc=new Scanner(System.in);
  7. System.out.println("Enter a string");
  8. String input=sc.nextLine();
  9. System.out.println("Number of vowels = "+count(input));
  10. }
  11. static int count(String s)
  12. {
  13. int cnt=0,i;
  14. for(i=0;i<s.length();i++)
  15. {
  16. if(isVowel(s.charAt(i)))
  17. cnt++;
  18. }
  19. return cnt;
  20. }
  21. //here is the isVowel method
  22. static boolean isVowel(char ch)
  23. {
  24. if(ch=='a' || ch=='A' || ch=='e' || ch=='E' || ch=='i' || ch=='I' || ch=='o' || ch=='O' || ch=='u' || ch=='U')
  25. return true;
  26. else
  27. return false;
  28. }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment