Looping through an array of strings and counting the number of times a vowel is in each word

76 Views Asked by At

Hi I'm trying to loop through an array of strings in count the number of times a vowel is in a word from the sentence.

String st = "Hello Today Is a lovely day";
String[] st1 = st.split(" ");
    
//for(int i = 0; i < st1.length; i++) {
//  System.out.println(st1[i]);
//}
    
String vo = "AaEeIiOoUu";
for(int k = 0; k < st1.length; k++) {
    int num = 0;
    for(int h = 0; h < st1.length; h++) {
        System.out.println(st1[k].charAt(h));
        if(st1[k].charAt(h).equals(vo.charAt(h))) {
            num++;
        }
    }
}

I can't figure out how to loop through each index of the array does anyone have any idea

3

There are 3 best solutions below

0
Anthony Zappin On

There are a few issues and improvements that can be made to your code. I think this is what you need to do to solve the problem:

  1. Split the sentence into words: It looks like you've done this correctly with st.split(" ").
  2. Then you will need to loop through each word. Again, it looks like you've set this up correctly with the outer loop for(int k = 0; k < st1.length; k++).
  3. Next, you will need to count vowels in each word. There are a few problems in this part of your code that need to be addressed.

Below is modified code that should solve your problem:

public class Main {
    public static void main(String[] args) {
        String st = "Hello Today Is a lovely day";
        String[] words = st.split(" ");
        
        String vowels = "AaEeIiOoUu";
        
        for(int k = 0; k < words.length; k++) { 
            int num = 0; 
            
            for(int h = 0; h < words[k].length(); h++) {
                char currentChar = words[k].charAt(h); 
                
                if(vowels.indexOf(currentChar) != -1) {
                    num++;
                }
            }
            
            System.out.println(words[k] + " has " + num + " vowels.");
        }
    }

}

This is a pretty basic solution. I think it can be further optimized, but it should get you started. Hopefully this helps.

0
prasad_ On

You are looping over the words in a sentence and then again looping over each of the word characters. Within the loop of each word characters, you can check if the word character has vowels and count those. Here is an approach:

String st = "Hello Today Is a lovely day";
String [] words  = st.split(" ");
String vowels = "AaEeIiOoUu";

for (int i = 0; i < words.length; i++) {

    String word = words[i]; 
    String [] wordChars = word.split("");
    int vCount = 0;

    for (int j = 0; j < wordChars.length; j++) {
        if (vowels.contains(wordChars[j])) {
            vCount = vCount + 1;
        }
    }
    
    System.out.println(word + " => " + vCount);
}

Note the usage of the String class method contains in the above code.

0
Bohemian On

The sum of the number of vowels in each word is the same as the number of vowels in the sentence, so don't split into words; just count the vowels.

The easiest way to do that is to remove non-vowels (by replacing them with blank) and the length of what's left is the number of vowels.

One line of code is all you need:

int num = st.replaceAll("[^AaEeIiOoUu]", "").length();

The search expression "[^AaEeIiOoUu]" is regex for "any character that is not a vowel".