PackageCom.xiaowu.demo;/*** Enter a line of characters to count the number of letters, spaces, numbers, and other characters in each of them. * * @authorWQ **/ Public classDemo7 { Public Static voidMain (string[] args) {String s= "Hello world! Beijing Aoyun 2008. "; Number (s); } Public Static voidNumber (String s) {intDigital = 0; intCharacter = 0; intother = 0; intBlank = 0; Char[] ch =S.tochararray (); for(inti = 0; i < ch.length; i++) { if(Ch[i] >= ' 0 ' && ch[i] <= ' 9 ') {Digital++; } Else if((Ch[i] >= ' A ' && ch[i] <= ' z ') | | ch[i] > ' A ' && ch[i] <= ' z ') {character++; } Else if(Ch[i] = = ") {Blank++; } Else{ Other++; }} System.out.println ("The string to test is:" +s); System.out.println ("Number:" + Digital + "\ n the number of English letters:" +character+ "\ n number of spaces:" + blank + "\ n Other characters:" +Other ); }}
Java Common algorithm questions (vii)