1. 程式人生 > >Java中判斷一句英文中有多少個以p開頭的單詞

Java中判斷一句英文中有多少個以p開頭的單詞

package Pak01;
public class TestString {
public static void main(String[] args) { 
    String s="peter piper picked a peck of pickled peppers";
    System.out.println(s);
    //將這些單詞以空格進行拆分放進一個String型別的字串數組裡
    String[] words=s.split(" ");
    int count=0;
    for(int i=0;i<words.length;i++) {
    String word=words[i];
    //判斷每個單詞的首字母
    if(word.charAt(0)=='p') {
    count++;
    }
    }
    System.out.println(count);
    }
}