2012-01-19 19 views
11

wordCountというメソッドを記述します。このメソッドは、Stringをパラメーターとして受け取り、String内の単語数を返します。単語は、1つ以上の空白以外の文字( ''以外の任意の文字)のシーケンスです。たとえば、call wordCount( "hello")は1を返し、call wordCount( "どうですか?")は3を返し、call wordCount( "この文字列はワイドスペースを持つ")は5を返し、call 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; 
} 

をしかし、私は、これは、文字列内のすべての単語は、NADを終了した後、それはまた、スペースの数をカウントすることを1つの制限があります知っています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-はtext = ""で失敗します。 –

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.trim().replaceAll("\\s+"," ").split(" ").length

+0

"この文字列にはスペースがありません" – Qwerky

+0

これは複数の空白文字s.replaceAll( "\\ s +"、 "").split( "")の場合に機能します。長さは –

0

があなたのコードにいくつかの行を追加しました使用... s.split(" ").lengthと広いスペースのために使用します。

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

単語の間に1つ以上の連続する空白がある場合、解決は失敗します。 – user3366706

関連する問題