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
What is flag in python?
What is a function in programming?
Write a program to print 15 random numbers using foreach of java 8?
What technique can be employed to compare two strings?
What is the difference between the paint() and repaint() methods?
What is a java string?
Are generics important java?
What are the different tags provided in jstl?
What is the difference between stored procedure & function?
Explain reverse a linked list iterative solution in java?
Does java trim remove newline?
Can an object subclass another object?
How to retrieve data from database in java using arraylist?
How many JVMs can run on a single machine and what is the Just-In-Time(JIT) compiler?
What do you mean by pointer value and address?