JavaAlgorithms/Others/CountWords.java

45 lines
1.3 KiB
Java
Raw Normal View History

package Others;
import java.util.Scanner;
/**
2020-10-24 18:23:28 +08:00
* You enter a string into this program, and it will return how many words were in that particular
* string
*
* @author Marcus
*/
public class CountWords {
2020-10-24 18:23:28 +08:00
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your text: ");
String str = input.nextLine();
2020-10-24 18:23:28 +08:00
System.out.println("Your text has " + wordCount(str) + " word(s)");
System.out.println("Your text has " + secondaryWordCount(str) + " word(s)");
input.close();
}
2020-10-24 18:23:28 +08:00
private static int wordCount(String s) {
if (s == null || s.isEmpty()) return 0;
return s.trim().split("[\\s]+").length;
}
2020-10-24 18:23:28 +08:00
/**
* counts the number of words in a sentence but ignores all potential non-alphanumeric characters
* that do not represent a word. runs in O(n) where n is the length of s
*
* @param s String: sentence with word(s)
* @return int: number of words
*/
private static int secondaryWordCount(String s) {
if (s == null || s.isEmpty()) return 0;
StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()) {
if (Character.isLetter(c) || Character.isDigit(c)) sb.append(c);
2019-08-28 10:01:09 +08:00
}
2020-10-24 18:23:28 +08:00
s = sb.toString();
return s.trim().split("[\\s]+").length;
}
}