Unused words in a given String in java
SOURCE : UnusedWords.javaimport java.util.ArrayList;import java.util.HashSet;import java.util.List;import java.util.StringTokenizer;public class UnusedWords { public static void main(String[] args) { String s = "To be or not to be - that is the question: Whether it is nobler in the mind to suffer, the slings and arrows of outrageous fortune. Or to take up arms against a sea of troubles, and by opposing end them"; StringTokenizer st = new StringTokenizer(s, " "); List ls = new ArrayList<>(); HashSet hst = new HashSet(); hst.add("a"); hst.add("be"); hst.add("that"); hst.add("the"); hst.add("this"); hst.add("or"); hst.add("to"); while (st.hasMoreTokens()) { String Currentword = st.nextToken(); if (!hst.contains(Currentword.toLowerCase())) { ls.add(Currentword.toLowerCase()); } } for (String srt : ls) { System.out.println(srt.toString()); } }} INPUTString s = "To be or not to be - that is the question: Whether it is nobler in the mind to suffer, the slings and arrows of outrageous fortune. Or to take up arms against a sea of troubles, and by opposing end them";OUTPUT
not-isquestion:whetheritisnoblerinmindsuffer,slingsandarrowsofoutrageousfortune.takeuparmsagainstseaoftroubles,andbyopposingendthem

0 comments :
Post a Comment