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

猜你喜欢

转载自blog.csdn.net/qq_41941875/article/details/79809611