Reputation: 239
Write a method called wordCount that accepts a String as its parameter and returns the number of words in the String. A word is a sequence of one or more nonspace characters (any character other than ' '). For example, the call wordCount("hello") should return 1, the call wordCount("how are you?") should return 3, the call wordCount(" this string has wide spaces ") should return 5, and the call wordCount(" ") should return 0.
I made a function:
public static int wordCount(String s){
int counter = 0;
for(int i=0; i<=s.length()-1; i++) {
if(Character.isLetter(s.charAt(i))){
counter++;
for(i<=s.length()-1; i++){
if(s.charAt(i)==' '){
counter++;
}
}
}
}
return counter;
}
But i know this has 1 limitation that it will also count the number of spaces after all the words in the string have finished nad it will also count 2 blank spaces as possibly being 2 words :( Is there a predefined function for word count? or can this code be corrected?
Upvotes: 12
Views: 56369
Reputation: 3471
My few solutions:
public static int wordcount1(String word) {
if (word == null || word.trim().length() == 0) {
return 0;
}
int counter = 1;
for (char c : word.trim().toCharArray()) {
if (c == ' ') {
counter++;
}
}
return counter;
}
//
public static int wordcount2(String word) {
if (word != null || word.length() > 0) {
return word.trim().length()
- word.trim().replaceAll("[ ]", "").length() + 1;
} else {
return 0;
}
}
// Recursive
public static int wordcount3(String word) {
if (word == null || word.length() == 0) {
return 0;
}
if (word.charAt(0) == ' ') {
return 1 + wordcount3(word.substring(1));
}
return wordcount3(word.substring(1));
}
//
public static int wordcount4(String word) {
if (word == null || word.length() == 0) {
return 0;
}
String check = word.trim();
int counter = 1;
for (int i = 0; i < check.length(); i++) {
if (i > 0 && Character.isSpaceChar(check.charAt(i))
&& !Character.isSpaceChar(check.charAt(i - 1))) {
counter++;
}
}
return counter;
}
Upvotes: 0
Reputation: 21
public static int wordCount(String s){
int counter=0;
for(int i=0;i<=s.length()-1;i++){
if(Character.isLetter(s.charAt(i))){
counter++;
for(;i<=s.length()-1;i++){
if(s.charAt(i)==' '){
i++;
break;
}
}
}
}
return counter;
}
This is what you need if don't use the predefined function. I have tested it by myself. Please let me know if there is any bugs!
Upvotes: 1
Reputation: 533492
If you want to ignore leading, trailing and duplicate spaces you can use
String trimmed = text.trim();
int words = trimmed.isEmpty() ? 0 : trimmed.split("\\s+").length;
Upvotes: 33
Reputation: 8530
String str="I am a good boy";
String[] words=str.split("\\s+");
System.out.println(words.length);
Upvotes: 2
Reputation: 3996
public static int wordCount(String s){
if (s == null)
return 0;
return s.trim().split("\\s+").length;
}
Have fun with the function.
Upvotes: 6
Reputation: 8101
Simply use s.split(" ").length
and for wide spaces...use s.trim().replaceAll("\\s+"," ").split(" ").length
Upvotes: 1
Reputation: 3065
Added some lines to your code:
public static int wordCount(String s){
int counter=0;
for(int i=0;i<=s.length()-1;i++){
if(Character.isLetter(s.charAt(i))){
counter++;
for(;i<=s.length()-1;i++){
if(s.charAt(i)==' '){
counter++;
i++;
while (s.charAt(i)==' ')
i++;
}
}
}
}
return counter;
}
Upvotes: 0
Reputation: 784998
It should be easy with:
String[] arr = "how are you sir".split("\\s");
System.out.printf("Count [%d]%n", arr.length);
Upvotes: 0
Reputation: 3572
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)
Splits this string around matches of the given regular expression.
Upvotes: 0