Skip to content

Instantly share code, notes, and snippets.

@leosabbir
Last active May 14, 2018 22:04
Show Gist options
  • Save leosabbir/2a01cba34e2374933da7ab5c4d538d34 to your computer and use it in GitHub Desktop.
Save leosabbir/2a01cba34e2374933da7ab5c4d538d34 to your computer and use it in GitHub Desktop.
A class to count number of words in an input string
/* File: WordCounter.java
* Created: 5/14/2018
* Author: Sabbir Manandhar
*
* Copyright (c) 2018 Hogwart Inc.
*/
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Utility Class to capture the words in the given input string delimited
* by pre-defined delimiters
*
* @author Sabbir Manandhar
* @version 1.0
*/
public class WordCounter {
private static Set<Character> delimiters = new HashSet<>();
static {
delimiters.add(' ');
delimiters.add('\'');
delimiters.add('!');
delimiters.add('-');
delimiters.add('(');
delimiters.add(')');
delimiters.add('{');
delimiters.add('}');
delimiters.add('[');
delimiters.add(']');
delimiters.add('?');
delimiters.add('/');
delimiters.add('\\');
delimiters.add(',');
}
//---------------------------------------------------------------------------------------------
/**
* driver method
* @param args
*/
public static void main(String[] args) {
System.out.println(count("I am lord,voldemort")); // 4
System.out.println(getWords("I am lord,voldemort")); // 4
System.out.println(count("I am lord voldemort ")); // 4
System.out.println(getWords("I am lord voldemort ")); // 4
System.out.println(count(" I am lord voldemort")); // 4
System.out.println(getWords(" I am lord voldemort")); // 4
System.out.println(count(" I am lord voldemort ")); // 4
System.out.println(getWords(" I am lord voldemort ")); // 4
System.out.println(count(" Lord Voldemort! is my past, my present and my future!!!! ")); // 10
System.out.println(getWords(" Lord Voldemort! is my past, my present and my future!!!! ")); // 4
} // main
//---------------------------------------------------------------------------------------------
/**
* Counts the number of words in the given input string
* @param input input string
* @return number of words in the input
*/
public static int count(String input) {
boolean delimiter = true;
int count = 0;
for (int i = 0; i < input.length(); i++) {
if (delimiters.contains(input.charAt(i))) { // delimiter encountered
delimiter = true;
} else {
if (delimiter) { // first non delimiter encountered after sequence of delimiters, increase word count
count++;
}
delimiter = false;
}
}
return count;
} // count
//---------------------------------------------------------------------------------------------
/**
* Gets the list words in the input string
* @param input input string
* @return List of words in the input
*/
public static List<String> getWords(String input) {
List<String> result = new ArrayList<>();
boolean delimiter = true;
//int count = 0;
int wordStart = 0;
for (int i = 0; i < input.length(); i++) {
if (delimiters.contains(input.charAt(i))) { // delimiter encountered
if (!delimiter) { // first delimiter after sequence of non delimiters, capture word
result.add(input.substring(wordStart, i));
}
delimiter = true;
} else {
if (delimiter) {
wordStart = i;
//count++;
}
delimiter = false;
}
}
if (!delimiter) {
result.add(input.substring(wordStart));
}
return result;
} // getWords
//---------------------------------------------------------------------------------------------
} // WordCounter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment