ALLInterview.com :: Home Page            
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
Google
   
 
Categories >> Software >> Java Related >> Java J2EE >> Core Java
 
 


 

 
 Core Java interview questions  Core Java Interview Questions (1721)
 Advanced Java interview questions  Advanced Java Interview Questions (331)
 Swing interview questions  Swing Interview Questions (44)
 EJB interview questions  EJB Interview Questions (134)
 Servlets interview questions  Servlets Interview Questions (255)
 Struts interview questions  Struts Interview Questions (196)
 JDBC interview questions  JDBC Interview Questions (187)
 JMS interview questions  JMS Interview Questions (9)
 SunOne interview questions  SunOne Interview Questions (3)
 J2EE interview questions  J2EE Interview Questions (71)
 Weblogic interview questions  Weblogic Interview Questions (146)
 Websphere interview questions  Websphere Interview Questions (14)
 Java Networking interview questions  Java Networking Interview Questions (20)
 Java J2EE AllOther interview questions  Java J2EE AllOther Interview Questions (87)
Question
Write a java program to get a string of words and print the numbers of each word count in descending order
 Question Submitted By :: Core-Java
I also faced this Question!!     Answer Posted By  
 
Answer
# 1
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 ?    23 Yes 4 No
Gdk
 
Answer
# 2
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 ?    11 Yes 3 No
Ruchira
 
 
 
Answer
# 3
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 ?    11 Yes 5 No
Anjan Singh
 
Answer
# 4
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 ?    11 Yes 6 No
Shubhasish
 
Answer
# 5
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 0 No
Surya
 
Answer
# 6
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 ?    7 Yes 9 No
Prash
 
Answer
# 7
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 ?    1 Yes 3 No
Pradeepta Kumar Mishra
 
Answer
# 8
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 ?    19 Yes 23 No
Hema
 
Answer
# 9
package com.subex.exm;




/**
* Author-Hema
* => Program that prints the nos. of each word count of
String in Descending order!!!
*/


public class subexExp {
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);
System.out.println(s.charAt(i-1));
}
}
}
 
Is This Answer Correct ?    2 Yes 6 No
Karthick
 
Answer
# 10
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 ?    21 Yes 26 No
Hema Talele
 

 
 
 
Other Core Java Interview Questions
 
  Question Asked @ Answers
 
what are Checked & Unchecked exceptions ?   2
What is the difference between Java Bean and Java Class.?   2
what is difference between class and object? HCL 43
how to handled exceptions & erros in ejb? Satyam 1
explain about jre and jvm   2
why java does compile time polymorphism at run time ? TCS 4
HOW TO PRINT A NO IN WORDS USING WHILE LOOP THE NO WILL BE PRINTED WHEN IT WILL BE IN THE RANGE BETWEEN 1 AND 3?   1
What is a J2EE component? List out all the component? Adobe 2
Which of the following can be referenced by a variable? A. The instance variables of a class only B. The methods of a class only C. The instance variables and methods of a class   2
Can we extend the String class?   3
Differentiate Vector and ArrayList? Wipro 4
String is a immutable objects . it means that string does not change........... But it will be chang......... { String s="kapil"; String s1="raj"; String s=s1; then print(.......) The String has been changed .. how it is possible and why its called immutable objects   7
 
For more Core Java Interview Questions Click Here 
 
 
 
 
 


   
Copyright Policy  |  Terms of Service  |  Articles  |  Site Map  |  RSS Site Map  |  Contact Us
   
Copyright 2013  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  KalAajKal.com