Write a Java method to count all vowels in a string


Introduction

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. import java.util.Scanner;
  2. public class Javaexcercise {
  3. public static void main(String[] args)
  4. {
  5. Scanner in = new Scanner(System.in);
  6. System.out.print("Enter the string: ");
  7. String str1 = in.nextLine();
  8.  
  9. System.out.print("Number of Vowels in the string: " + countVowels(str1)+"\n");
  10. }
  11. public static int countVowels(String str1)
  12. {
  13. int count = 0;
  14. for (int i = 0; i < str1.length(); i++)
  15. {
  16. if (str1.charAt(i) == 'a' || str1.charAt(i) == 'e' || str1.charAt(i) == 'i'
  17. || str1.charAt(i) == 'o' || str1.charAt(i) == 'u')
  18. {
  19. count++;
  20. }
  21. }
  22. return count;
  23. }
  24. }

Result

java techstudy.org

     





Related Topics




Popular Posts