Title Description: Write the program, the output string of uppercase letters, lowercase small mother and the number of other. If there is a string "Helle, this is A test textfile.123456, Tannk you!!", the number of uppercase letters: 3, lowercase letters: 29, the number of other characters: 18.
Here are four algorithms, the first of which is a good understanding, but also a hard coding problem, and the other three ways to use the Java-language JDK provided by the API.
Method One:
<! DOCTYPE html>
[Java]View PlainCopy
- Method One: In the use of Unicode code for each character between A~z, call the JDK
- The Charat of the string class is used to take out each character of the strings and compare them to determine
- Class Findletter {
- public static void Main (string[] args) {
- String str = "Helle, this is A test textfile.123456, Tannk you!!";
- int upcount = 0;
- int lowcount = 0;
- int othercount = 0;
- For (int i = 0; i < str.length (); i++) {
- char c = str.charat (i);
- if (c >= ' a ' && c <= ' Z ') {
- lowcount++;
- } Else if (c >= ' A ' && c <= ' Z ') {
- upcount++;
- } Else {
- othercount++;
- }
- }
- System.out.println ("Number of uppercase females:" + upcount);
- System.out.println ("number of lowercase letters:" + lowcount);
- System.out.println ("number of other characters:" + othercount);
- }
- }
Method Two:
[Java]View PlainCopy
- Method Two: The Isuppercase method and Islowercase method of character class using JDK
- Class FindLetter1 {
- public static void Main (string[] args) {
- String str = "Helle, this is A test textfile.123456, Tannk you!!";
- int upcount = 0;
- int lowcount = 0;
- int othercount = 0;
- For (int i = 0; i < str.length (); i++) {
- char c = str.charat (i);
- if (Character.isuppercase (c)) {
- upcount++;
- } Else if (Character.islowercase (c)) {
- lowcount++;
- } Else {
- othercount++;
- }
- }
- System.out.println ("number of capital letters:" + upcount);
- System.out.println ("number of lowercase letters:" + lowcount);
- System.out.println ("number of other letters:" + othercount);
- }
- }
Method Three:
[Java]View PlainCopy
- Method Three: First define two strings A to Z and A to Z, and then remove each letter of the STR string,
- Use the IndexOf () method to determine if the character is in this defined string, in the uppercase line,
- The counter of the capital letter is added 1, in the lowercase letter this line, the lowercase letter adds one, otherwise the other letter calculator
- Plus 1
- Class FindLetter2 {
- public static void Main (string[] args) {
- String low = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- String up = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- int lowcount = 0;
- int upcount = 0;
- int othercount = 0;
- String str = "Helle, this is A test textfile.123456, Tannk you!!";
- For (int i = 0; i < str.length (); i++) {
- char c = str.charat (i);
- if (Low.indexof (c)! =-1) {
- lowcount++;
- } Else if (Up.indexof (c)! =-1) {
- upcount++;
- } Else {
- othercount++;
- }
- }
- System.out.println ("number of capital letters:" + upcount);
- System.out.println ("number of lowercase letters:" + lowcount);
- System.out.println ("number of other letters:" + othercount);
- }
- }
Method Four:
[Java]View PlainCopy
- Convert str to uppercase and lowercase uppercase with su lowercase SL
- Then count the numbers by comparing them to the original string.
- Class FindLetter3 {
- public static void Main (string[] args) {
- String str = "Helle, this is A test textfile.123456, Tannk you!!";
- String SU = Str.touppercase ();
- String SL = Str.tolowercase ();
- int lowcount = 0;
- int upcount = 0;
- int othercount = 0;
- For (int i = 0; i < str.length (); i++) {
- char charstr = Str.charat (i);
- char Charsu = Su.charat (i);
- char charsl = Sl.charat (i);
- //If it is not a letter and is a different character, it is counted directly with Othercount
- if (Character.isletter (CHARSTR)) {
- //If the original string is equal to the converted uppercase string, the original character is uppercase,
- //lowercase letters if they are equal to lowercase letters
- if (charstr = = Charsu) {
- upcount++;
- } Else if (charstr = = charsl) {
- lowcount++;
- }
- } Else {
- othercount++;
- }
- }
- System.out.println ("number of capital letters:" + upcount);
- System.out.println ("number of lowercase letters:" + lowcount);
- System.out.println ("number of other letters:" + othercount);
- }
- }
All four of these algorithms have the correct output:
Number of capital letters: 3
Number of lowercase letters: 29
Number of other letters: 18
In a string, four algorithms for counting the number of uppercase letters, the number of lowercase letters, and the number of other characters