java 统计单词个数和标点符号
 把随机输入的一句话比如:It's only a test!存放在一个char[]的数组中,统计char[]中的单词个数和标点符号的个数。
把随机输入的一句话比如:It's only a test!存放在一个char[]的数组中,统计char[]中的单词个数和标点符号的个数。 package com.faintbear;
 
  import java.io.*;
import java.io.*;  public class Test{
public class Test{  public static void main(String[] args) throws Exception{
    public static void main(String[] args) throws Exception{  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  String str = br.readLine();
        String str = br.readLine();  if(str == null) throw new Exception("");
        if(str == null) throw new Exception("");  char[] c = str.toCharArray();
        char[] c = str.toCharArray();  int words = 0;
        int words = 0;  int ip = 0;
        int ip = 0;  boolean wordflag = false;
        boolean wordflag = false;  for(int i=0;i<c.length;i++){
        for(int i=0;i<c.length;i++){  if((c[i]>='a' && c[i] <= 'z') || (c[i] >= 'A' && c[i] <= 'Z')){
            if((c[i]>='a' && c[i] <= 'z') || (c[i] >= 'A' && c[i] <= 'Z')){  if(wordflag) {
                if(wordflag) {  continue;
                    continue;  }else{
                }else{  words++;
                    words++;  }
                }  wordflag = true;
                wordflag = true;  }else{
            }else{  wordflag = false;
                wordflag = false;  if(c[i] != ' ')
                if(c[i] != ' ')  ip++;
                    ip++;  }
            }  }
        }  System.out.println("words=" + words);
        System.out.println("words=" + words);  System.out.println("ip=" + ip);
        System.out.println("ip=" + ip);  for(int i=0;i<c.length;i++)
        for(int i=0;i<c.length;i++)  {
        {  System.out.print("c["+i+"]="+c[i]);
          System.out.print("c["+i+"]="+c[i]);  }
        }  
  }
    }  }
}



 
     
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号