码迷,mamicode.com
首页 > 编程语言 > 详细

Java程序员笔试面试之String5

时间:2014-11-03 21:00:14      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   java   for   sp   on   2014   

.如何统计String中有多少个单词?

public class CountWord {
      public static int countWord(String s){
    	  int count=0;
    	  int word=0;
    	  
    	  for(int i=0;i<s.length();i++){
    		  if("".equals(s.charAt(i)))
    			  word=0;
    		  else if(word==0) {
    			  word=1;
    			  count++;
    		  }
    	  }
		return count;
      }
      public static void main(String args[]){
    	  String s="I am Jiang wei wei";
    	  System.out.print(s);
    	  System.out.println("单词的个数为:"+countWord(s));
    	  
      }
}


Java程序员笔试面试之String5

标签:style   blog   color   ar   java   for   sp   on   2014   

原文地址:http://blog.csdn.net/dq3wrr/article/details/40746955

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!