统计字符
统计字符
http://www.nowcoder.com/questionTerminal/539054b4c33b4776bc350155f7abd8f5
四种情况,没什么难点。
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { String str = in.nextLine(); int letter = 0; int space = 0; int num = 0; int other = 0; for(int i = 0; i < str.length(); i++){ char c = str.charAt(i); if((c >= 'a' && c <= 'z')||(c >= 'A' && c <= 'Z')){ letter++; } else if(c >= '0' && c <= '9'){ num++; } else if(c == ' '){ space++; } else{ other++; } } System.out.println(letter); System.out.println(space); System.out.println(num); System.out.println(other); } } }