Sunday, September 26, 2021

Week one of practice questions

 Question 1 : Write a Java method to count all vowels in a string.

import java.util.Scanner;

public class Practice_Day_1 {

    static int countVowels(String str){
        int count = 0;
        for (int i = 0i < str.length(); i++) {
            if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i'
                    || str.charAt(i) == 'o' || str.charAt(i) == 'u' ||
                     str.charAt(i) == 'A' ||
                     str.charAt(i) == 'E' || str.charAt(i) == 'I'
                    || str.charAt(i) == 'O' || str.charAt(i) == 'U') {
                count = count + 1;
            }
        }
        return count;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a String");
        String str = sc.nextLine();
        System.out.println(countVowels(str));
    }
}

No comments:

Post a Comment