This program is a solution to this problem: http://uva.onlinejudge.org/external/4/494.pdf. The basic idea is given a sentence, you want to find out how many words are in the sentence.
Lots of examples online that I have found say to count the number of spaces. Though this isn't correct.
Consider this input:
Counting the spaces would say that there is 3 words. This is wrong. There is only 2. The ... doesn't count. Words can only consist of letters (upper and lower case), dashes and apostrophe's.Code:Hi ... test
The first thing my algorithm done is remove trailing white space, and all amounts of white space are replaced with just one space. Though this doesn't really matter very much. I also remove all numbers and characters that cannot be part of a word.
Now what I do is move along the string until I find a character that is not part of a word. A space indicates the termination of a word. At this point, we count the word. Then we move along until we either find the end of the string or a letter starting the next word.
Notice though that counting spaces can't work. Why consider this:
Counting the spaces will give us 3 words. This is wrong because '' isn't a valid word.Hi '' test.
Code:import java.util.*; import java.io.*; public class wordCount { public static void main(String[] args) throws IOException { Scanner fin = new Scanner(new FileReader("wordCount.txt")); //Scanner fin = new Scanner(System.in); String sLine = ""; int i = 0; int words = 0; // number of words while (fin.hasNext()) { words = 0; i = 0; sLine = fin.nextLine(); // words can only contain letters, single quotes, and dashes // so remove everything else sLine = sLine.replaceAll("[^A-Za-z\'-]", " ").trim(); // remove everything except for space sLine = sLine.replaceAll("\\s{1,}", " "); while (i < sLine.length()) { // keep increasing i until you find the end of the word // or the end of the string while (i < sLine.length() && (Character.isLetter(sLine.charAt(i)) || sLine.charAt(i) == '-' || sLine.charAt(i) == '\'')) { i++; } words++; // count the word // keep increasing i until you find the next word while (i < sLine.length() && !Character.isLetter(sLine.charAt(i))) { i++; } } System.out.println(words); } fin.close(); } }![]()


LinkBack URL
About LinkBacks





Reply With Quote


Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum