Ask AI on The Internet
Question: genorate a program in java that can count the number of vowels in a string called word.
Sure! Here's a program in Java that counts the number of vowels in a string: ``` import java.util.Scanner; public class CountVowels { public static int countVowels(String word) { int count = 0; for(int i=0;i<word.length();i++) { char c = word.charAt(i); if(c=='a' || c=='e' || c=='i' || c=='o' || c=='u' || c=='A' || c=='E' || c=='I' || c=='O' || c=='U') { count++; } } return count; } public static void main(String[] args) { System.out.print("Enter a word: "); Scanner scanner = new Scanner(System.in); String word = scanner.nextLine(); int count = countVowels(word); System.out.println("Number of vowels in " + word + ": " + count); } } ``` The `countVowels` method takes a `String` called `word` as input and returns an integer value that represents the count of vowels in the given `word`. The `for` loop is used to iterate over each character of the string, and the if condition is used to check if the character is a vowel or not. If it is a vowel, then the count is incremented. Finally, the `main` method is used to accept user input for a word and displays the output by calling the `countVowels` method. I hope this helps! Let me know if you have any questions.
March 24, 2023, 10:17 a.m.
A new large language model has been made available since this question was asked.
To generate an updated answer from the new model, click the button below.
Think your answer is better than the AI's? Post it below.
Other Most Relevant Posts:
If you want your question answered by an AI, click here.
Post your own comment: