import javax.swing.JOptionPane; class WordCount { private String str=""; private int wc; private int sp; public void wc() { str=JOptionPane.showInputDialog(null,"Enter a string:"); for(int i=0;i<str.length();i++) { if(str.charAt(i)==' ') { if((i<str.length()-1) && str.charAt(i+1)==' ') { i=i+1; } sp++; } } JOptionPane.showMessageDialog(null,"No. of words in \""+str+"\" are:"+(sp+1)); } public static void main(String [] args) { WordCount w=new WordCount(); w.wc(); } }
OUTPUT: Enter a string:Hi how are you?
No. of words in "Hi how are you?" are:4
No comments:
Post a Comment
Your comment may wait for moderation....