题解 | #统计字符#
统计字符
http://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5
JAVA
import java.util.* ; public class Main{ public static void main(String[] agrs){ Scanner sc = new Scanner(System.in); //Map<String,Integer> charMap = new HashMap<String,Integer>(); while(sc.hasNext()){ String inStr = sc.nextLine(); int engCharCount = 0; int spaceCharCount = 0; int numCharCount = 0; int otherCharCount = 0; //遍历输入的字符串 for(int i =0 ; i<inStr.length();i++){ //英文字符 if((inStr.charAt(i) >= 'a' && inStr.charAt(i)<= 'z') || (inStr.charAt(i) >= 'A' && inStr.charAt(i)<= 'Z')){ engCharCount ++ ; }else if(inStr.charAt(i) >= '0' && inStr.charAt(i)<= '9'){ numCharCount ++; }else if(inStr.charAt(i) == ' '){ spaceCharCount ++; }else{ otherCharCount ++; } } System.out.println(engCharCount); System.out.println(spaceCharCount); System.out.println(numCharCount); System.out.println(otherCharCount); } } }