belongs to collection: Method/Function programming exercises in Java
Write a Java method to count all vowels in a string
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..
import java.util.Scanner; public class Javaexcercise { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter the string: "); String str1 = in.nextLine(); System.out.print("Number of Vowels in the string: " + countVowels(str1)+"\n"); } public static int countVowels(String str1) { int count = 0; for (int i = 0; i < str1.length(); i++) { if (str1.charAt(i) == 'a' || str1.charAt(i) == 'e' || str1.charAt(i) == 'i' || str1.charAt(i) == 'o' || str1.charAt(i) == 'u') { count++; } } return count; } }
Result:
Enter the string: Tech Study - The complete debuggin solution
Number of Vowels in the string: 13
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
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..
Result:
Enter the string: Tech Study - The complete debuggin solution
Number of Vowels in the string: 13
need an explanation for this answer? contact us directly to get an explanation for this answer