Write a java program to get a string of words and print the numbers of each word count in descending order
Answers were Sorted based on User's Feedback
Answer / gdk
public static void main(String[] args) {
String word = "get the count and arrange in descending order";
String[] temp = word.split(" ");
for (int index = 0; index < temp.length; index++) {
for (int i = index+1; i < temp.length; i++) {
if (temp[i].length() > temp[index].length()) {
String word2 = temp[i];
temp[i] = temp[index];
temp[index] = word2;
}
}
System.out.println(temp[index] +" " +temp[index].length());
}
}
Is This Answer Correct ? | 28 Yes | 4 No |
Answer / ruchira
import java.util.StringTokenizer;
public class NumberOfWords {
/**
* to get a string of words and print the numbers of
each word count in descending order
*/
public static void main(String[] args) {
String str = "Save water";
System.out.println(str);
StringTokenizer st = new
StringTokenizer(str," ");
int i = 0;
i=st.countTokens();
while (i>0)
{
System.out.println(i);
i --;
}
}
}
Is This Answer Correct ? | 12 Yes | 3 No |
Answer / anjan singh
import java.io.*;
public class Sample {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
BufferedReader br= new BufferedReader
(new InputStreamReader(System.in));
System.out.println("enter a sentence:");
String str = br.readLine();
str = str.trim();
String[] strArr = str.split(" ");
for(int i=0;i<strArr.length;i++)
for(int
j=i+1;j<strArr.length;j++)
if(strArr[j].length()
> strArr[i].length())
{
String
x=strArr[j];
strArr[j]
=strArr[i];
strArr[i]=x;
}
System.out.println("output:-");
for(int i=0;i<strArr.length;i++)
System.out.println(strArr[i]);
}
catch(Exception e){}
}
}
Is This Answer Correct ? | 12 Yes | 5 No |
Answer / shubhasish
public class WordCount {
private String word;
private Long wordCount;
public WordCount(String word) {
this.word = word;
}
public void displayWordCount() {
String[] temp = word.split(" ");
for (int index = temp.length - 1; index >= 0; index--) {
System.out.println(temp[index] + "-->" +
temp[index].length());
}
}
public static void main(String args[]) {
WordCount wordCount = new WordCount("I Will Crack
This");
wordCount.displayWordCount();
}
}
Is This Answer Correct ? | 12 Yes | 8 No |
Answer / megha shetty
public class Inc {
public static void main(String[] args) {
// TODO Auto-generated method stub
String temp;
String s="this is my way of doing it";
String s1[]=s.split(" ");
for(int j=1;j<=s1.length-1;j++)
for(int i=0;i<s1.length-j;i++)
{
if(s1[i].length()<s1[i+1].length())
{ temp=s1[i];
s1[i]=s1[i+1];
s1[i+1]=temp;
}
}
System.out.println("
new sorted array:
");
for(String i:s1)
System.out.println(i+" the length is "+i.length());
}
}
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / 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 |
Answer / hema talele
package com;
public class StringChar {
/**
* @param Hema.talele
*/
public static void main(String[] args) {
String str = "HemaLovesMilind";
System.out.println(str);
for(int i= str.length(); i>0 ; i--)
{
System.out.println("char="+str.charAt(i-1));
System.out.println("index of
letter="+str.indexOf(str.charAt(i-1)));
System.out.println("simple count"+i);
System.out.println(str.charAt(i-1)+"="+i);
}
}
}
Is This Answer Correct ? | 24 Yes | 26 No |
Answer / prash
class WordCount{
public List list;
public static void main(String[] args) {
System.out.println(args[0]);
List arr = Arrays.asList(args);
Iterator it = arr.iterator();
List l = new ArrayList();
while (it.hasNext()) {
String s = (String) it.next();
System.out.println(s);
l.add(s.length());
}
Collections.sort(l);
Collections.reverse(l);
}
}
Is This Answer Correct ? | 8 Yes | 10 No |
Answer / surya
public class WordCount {
public static void main(String[] args) {
String string="I got a simple solution";
String[] temp=string.split(" ");
for (int i = temp.length-1; i >=0 ; i--) {
System.out.println(temp[i]+ "-->"+temp[i].length());
}
}
}
Is This Answer Correct ? | 0 Yes | 2 No |
Answer / hema
package sampleques;
/**
* Author-Hema
* => Program that prints the nos. of each word count of
String in Descending order!!!
*/
public class CountStringWords {
public static void main(String[] args) {
String s="HemaLovesMilind";
System.out.println(s);
for(int i=s.length();i>0;i--)
{
System.out.println(i );
}
}
}
Is This Answer Correct ? | 20 Yes | 23 No |
what is j2se
Why is serialization required?
What is an array and a vector? How they different from each other?
how to run servlet program between two computer through the internet ?
2 Answers Kiran Prakashan, TCS,
Difference between a process and a program?
What are uses of Hash Code?
What is local variable and instance variable?
Why put method is idempotent?
what happens when we add the objects morethan the size limit to a hashmap
What is variable in java?
Differences between traditional programming language and object oriented programming language?
What is the multi-catch block in java?