Write a java program to get a string of words and print the numbers of each word count in descending order
Answer Posted / pradeepta kumar mishra
package interview;
import java.util.*;
import java.io.*;
class WordCount{
public static void main(String[] args) {
int count =0;
try{
BufferedReader br= new BufferedReader(new
InputStreamReader(System.in));
System.out.println("enter a sentence:");
String str = br.readLine();
List<Integer> l = new ArrayList<Integer>();
for(int k=str.length();k>=1;k--)
{
if(str.charAt(k-1)==' ')
{
l.add(count);
count=-1;
}
count++;
}
l.add(count);
Comparator<Integer> comparator =
Collections.reverseOrder();
Collections.sort(l,comparator);
System.out.println("Number of each word in
desc order --"+l);
}
catch(Exception e){
}
}
}
| Is This Answer Correct ? | 2 Yes | 3 No |
Post New Answer View All Answers
Explain the difference between call by refrence and call by value?
What is starvation?
Differences between traditional programming language and object oriented programming language?
Difference difference paint() and paintcomponent()?
give an example for encapsulation?
What are the common uses of "this" keyword in java ?
Why is it important to initialize a variable?
What does the three dot emoji mean?
What is the syntax and characteristics of a lambda expression? Explain
Can we increase size of array?
Are private methods final?
What are the problems faced by java programmers who don't use layout managers?
Why we cannot override static method?
What is meant by javabeans?
Is there any difference between nested classes and inner classes?