java 统计单词个数和标点符号

http://www.blogjava.net/faintbear/archive/2008/09/03/2872.html



import java.io.*;
public class Test{
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        if(str == null) throw new Exception("");
        char[] c = str.toCharArray();
        int words = 0;
        int ip = 0;
        boolean wordflag = false;
        for(int i=0;i<c.length;i++){
            if((c[i]>='a' && c[i] <= 'z') || (c[i] >= 'A' && c[i] <= 'Z')){
                if(wordflag) {
                    continue;
                }else{
                    words++;
                }
                wordflag = true;
            }else{
                wordflag = false;
                if(c[i] != ' ')
                    ip++;
            }
        }
        System.out.println("words=" + words);
        System.out.println("ip=" + ip);
        for(int i=0;i<c.length;i++)
        {
          System.out.print("c["+i+"]="+c[i]);
        }

    }
}

猜你喜欢

转载自panyongzheng.iteye.com/blog/1569805