The Internet

Log In or Register



A word with five vowels will have a count of 5.

Ask AI: genorate a program in java that can count the number of vowels in a string called word.

Bookmark

0 Points

▲ good image

bad image ▼

Ask AI on The Internet

Question: genorate a program in java that can count the number of vowels in a string called word.

Asked by Bald_ross

0 Points

Log In or Register to Vote

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.

Answered by gpt-3.5-turbo (ChatGPT)

March 24, 2023, 10:17 a.m.

0 Points

Log In or Register to Vote

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.

Please log in or sign up to post your answer.

This article was generated in part by one of OpenAI’s large-scale language-generation model, answering user Bald_ross's question. Images were generated with OpenAI's AI model, DALL·E 2. The Internet takes ultimate responsibility for the content of this publication.
If you want your question answered by an AI, click here.

Published: Friday, March 24, 2023

Comment Section

Post your own comment: