java 统计单词个数和标点符号
http://www.blogjava.net/faintbear/archive/2008/09/03/2872.html
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
Java代码 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]); } } } |
