Q:

Write a Java method to count all vowels in a string

0

Write a Java method to count all vowels in a string

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write a Java method to compute the sum of the digi... >>
<< Write a Java method to count all words in a string...