Answers for "how to count vowels in string in java"

2

count vowels in a string java

// There are many ways to do it the one i came up with is this:  
int count = 0;
String sentence = "My name is DEATHVADER, Hello World!!! XD";
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
for (char vowel : vowels) 
  for (char letter : sentence.toLowerCase().toCharArray()) 
    if (letter == vowel) count++;
System.out.println("Number of vowels in the given sentence is " + count);

// I have found another easier method if you guyz don't understand this
// but this is also easy xD.
// here you can see another method:
// https://www.tutorialspoint.com/Java-program-to-count-the-number-of-vowels-in-a-given-sentence
Posted by: Guest on May-15-2021
0

print vowels in string java

 
package org.arpit.java2blog;
 
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
 
public class VowelFinder
{
    public static void main(String args[])
    {
        Scanner scanner = new Scanner(System.in);
 
        System.out.print("Enter an String : ");
        String str = scanner.next();
 
        Set<Character> set=new HashSet<Character>();
        for (int i = 0; i < str.length(); i++) {
            char c=str.charAt(i);
            if(isVowel(c))
            {
                set.add(c);
            }
        }
 
        System.out.println("Vowels are:");
        for (Character c:set) {
            System.out.print(" "+c);
        }
 
        scanner.close();
    }
 
    public static boolean isVowel(char character)
    {
 
        if(character=='a' || character=='A' || character=='e' || character=='E' ||
                character=='i' || character=='I' || character=='o' || character=='O' ||
                character=='u' || character=='U'){
            return true;
        }else{
            return false;
        }
    }
 
}
 
Posted by: Guest on August-02-2021

Code answers related to "how to count vowels in string in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language