题目:计算首末不含空格各个子串之间只含一个空格的字符串中子串出现的次数
分析:例如输入的字符串为"I come from County DingYuan Province AnHui."
空格隔断的即为字符子串,所以上述字符串的子串个数有7个
public class Prog49{
public static void main(String[] args){
String str="I come from County DingYuan Province AnHui.";
int count=0;
char[] ch=str.toCharArray();//将字符串转换成字符数组
for(int i=0;i<ch.length;i++) {
if(ch[i]==' ')//计算字符串中的空格个数
count++;
}
count++;//字符串中子串的个数=空格数+1
System.out.println("共有"+count+"个字符子串");
}
}
/*运行结果
共有7个字符子串
*/