Write a program (method main) using an array to solve the following problem: Pro
ID: 3574403 • Letter: W
Question
Write a program (method main) using an array to solve the following problem: Prompt the user to enter a String of data Using the charAt(index) method in the String class to access each character in the string individually, produce a count of the number of word lengths Count only letters (regardless of upper or lower case) and treat spaces, numbers and punctuation as word separators. Display the number of words and lengths for all non-zero lengths, and the total number of words Enter the phrase: Pneumonoultramicroscopicsilicovolcanoconiosis is the longest word in the dictionary 2 2 letter words 2 3 letter words 1 4 letter words 1 7 letter words 1 10 letter words 45 letter words 8 total words Enter the phrase: one, two, three, four, five, six, seven, eight, nine, ten, 1234567890!@#$%^&*()_+ 4 3 letter words 3 4 letter words 3 5 letter words 10 total words Enter the phrase: The Quick Brown Fox Jumps Over The Lazy Dog 4 3 letter words 2 4 letter words 3 5 letter words 9 total wordsExplanation / Answer
import java.util.*;
public class wordcount {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter string or sentance:");
String sentence=sc.nextLine();
int count=0;
int word=0;
for(int i=0;i<sentence.length();i++){
if(sentence.charAt(i)==' '||sentence.charAt(i)=='!'||(sentence.charAt(i)>48&&sentence.charAt(i)<57)){
count++;
}else{
word++;
}
}
System.out.println("total words:"+(count+1));
}
}
output:
enter string or sentance:
shsh hshsh hshs2hdhdh!
total words:5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.