public class WordNumberString {
//定義済みの関数を使用 split
static int numberwords(String string){
int n=0;
文字列[]単語;
/split は文字列を単語のセットに分割します
//スペースがある場合、それらを配列に入れます
words = string.split(" ");
returnの単語.length;
}
//定義済み関数なし split
static int nombremots_naive(String string){
int n=0;
char c;
//文字列全体を参照
for(int i = 0 ; i < string.length() ; i++){
//文字ごとに
c = string.charAt(i);
//スペースを数えないようにする
/最初と最後に
//例: "私はプログラマーです"
//このテストを削除すると、単語数
//単語が4つあっても6になります.
if(i!=0 &&i!=string.length()-1)
if(c==' ')
n++;
}
//文字列が空でない場合
//次のため1を加算します
//間のスペースをカウントします
//単語、つまりスペースが3つある場合
//4つの単語になります
if(n>1)
n++;
return n;
}
public static void main(String[] args) {
String string="私はプログラマーです";
System.out.println(nombremots_naive(文字列));
}
}
Please disable your ad blocker and refresh the window to use this website.