2012-01-19 72 views
11

编写一个名为wordCount的方法,它接受一个字符串作为其参数并返回字符串中的字数。一个单词是一个或多个非空格字符(除''之外的任何字符)的序列。例如,调用wordCount(“hello”)应该返回1,调用wordCount(“你好吗?”)应该返回3,调用wordCount(“this string has wide spaces”)应该返回5,调用wordCount (”“)应该返回0如何计算单词之间有空单词的字符的确切数量?

我做了一个功能:

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; 
} 

但我知道这有1个限制,这也将所有的字符串中的单词完成后NAD计数的空格数它也将计算2个空格,可能是2个单词:( 是否有预定义的字数计数功能?或者可以更正此代码吗?

+0

你就不能这样做'字符串#分裂(” \\ s“);'并获得结果数组的长度。 – anubhava

+4

你是否错过了'[作业]'标签? –

回答

30

如果你想忽略你可以使用的前导,尾随和重复空格

String trimmed = text.trim(); 
int words = trimmed.isEmpty() ? 0 : trimmed.split("\\s+").length; 
+0

第一个答案考虑到连续多个空格+1。 –

+1

和领先的空间。 split()默认会忽略尾随分隔符。 –

+0

@ PeterLawrey--文本失败=“”; –

2
String str="I am a good boy"; 
String[] words=str.split("\\s+"); 
System.out.println(words.length); 
+0

你试过类似“foo_______bar”吗? –

+2

“这个字符串有很大的空间”失败 – Qwerky

+0

@qwerty +1 tx fr –

0

它应该是容易:

String[] arr = "how are you sir".split("\\s"); 
System.out.printf("Count [%d]%n", arr.length); 
0

只需使用s.split(" ").length和宽敞的空间...使用s.trim().replaceAll("\\s+"," ").split(" ").length

+0

“这个字符串有很大的空间”失败 – Qwerky

+0

这将工作在多个空格的情况下s.replaceAll(“\\ s +”,“”).split(“”)。长度为 –

0

增加了一些行到你的代码:

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; 
    } 
5
public static int wordCount(String s){ 
    if (s == null) 
     return 0; 
    return s.trim().split("\\s+").length; 
} 

玩得开心的功能。

0
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; 
} 

这是你所需要的,如果不使用预定义的功能。我已经自己测试过了。请让我知道是否有任何错误!

0

我的几个解决方案:

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; 
     } 
    } 

//递归

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; 
    } 
+0

如果单词之间存在多个连续空格,则解决方案将失败。 – user3366706

相关问题