Write a program (method main) using an array to solve the following problem: Pro
ID: 3779255 • 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 Example 1: (green is user input) 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 1 45 letter words 8 total words Example 2: 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 Example 3: 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 words
Explanation / Answer
Answer:
import java.util.*;
class Wordlength{
public static void main(String[] args){
String temp;
Scanner read=new Scanner(System.in);
System.out.print("Enter a String :");
temp=read.nextLine();
String[] input = temp.split(" ");
String str = "";
int [] len = new int[input.length];
int [] increment = new int[input.length];
int c = 0;
for(int i=0;i<input.length;i++){
str = input[i];
len[i] = str.length();
for(int j=0;j<input.length;j++){
if(str.length() == input[j].length()){
increment[i] = ++c;
}
}
c = 0;
}
for(int i=0;i<len.length;i++){
System.out.println("No. of words of length "+len[i]+" are
"+increment[i]+".");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.