In a string, four algorithms for counting the number of uppercase letters, the number of lowercase letters, and the number of other characters

Source: Internet
Author: User
Tags alphanumeric characters

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
  1. Method One: In the use of Unicode code for each character between A~z, call the JDK
  2. The Charat of the string class is used to take out each character of the strings and compare them to determine
  3. Class Findletter {
  4. public static void Main (string[] args) {
  5. String str = "Helle, this is A test textfile.123456, Tannk you!!";
  6. int upcount = 0;
  7. int lowcount = 0;
  8. int othercount = 0;
  9. For (int i = 0; i < str.length (); i++) {
  10. char c = str.charat (i);
  11. if (c >= ' a ' && c <= ' Z ') {
  12. lowcount++;
  13. } Else if (c >= ' A ' && c <= ' Z ') {
  14. upcount++;
  15. } Else {
  16. othercount++;
  17. }
  18. }
  19. System.out.println ("Number of uppercase females:" + upcount);
  20. System.out.println ("number of lowercase letters:" + lowcount);
  21. System.out.println ("number of other characters:" + othercount);
  22. }
  23. }

Method Two:

[Java]View PlainCopy
  1. Method Two: The Isuppercase method and Islowercase method of character class using JDK
  2. Class FindLetter1 {
  3. public static void Main (string[] args) {
  4. String str = "Helle, this is A test textfile.123456, Tannk you!!";
  5. int upcount = 0;
  6. int lowcount = 0;
  7. int othercount = 0;
  8. For (int i = 0; i < str.length (); i++) {
  9. char c = str.charat (i);
  10. if (Character.isuppercase (c)) {
  11. upcount++;
  12. } Else if (Character.islowercase (c)) {
  13. lowcount++;
  14. } Else {
  15. othercount++;
  16. }
  17. }
  18. System.out.println ("number of capital letters:" + upcount);
  19. System.out.println ("number of lowercase letters:" + lowcount);
  20. System.out.println ("number of other letters:" + othercount);
  21. }
  22. }


Method Three:

[Java]View PlainCopy
  1. Method Three: First define two strings A to Z and A to Z, and then remove each letter of the STR string,
  2. Use the IndexOf () method to determine if the character is in this defined string, in the uppercase line,
  3. 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
  4. Plus 1
  5. Class FindLetter2 {
  6. public static void Main (string[] args) {
  7. String low = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  8. String up = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  9. int lowcount = 0;
  10. int upcount = 0;
  11. int othercount = 0;
  12. String str = "Helle, this is A test textfile.123456, Tannk you!!";
  13. For (int i = 0; i < str.length (); i++) {
  14. char c = str.charat (i);
  15. if (Low.indexof (c)! =-1) {
  16. lowcount++;
  17. } Else if (Up.indexof (c)! =-1) {
  18. upcount++;
  19. } Else {
  20. othercount++;
  21. }
  22. }
  23. System.out.println ("number of capital letters:" + upcount);
  24. System.out.println ("number of lowercase letters:" + lowcount);
  25. System.out.println ("number of other letters:" + othercount);
  26. }
  27. }


Method Four:

[Java]View PlainCopy
  1. Convert str to uppercase and lowercase uppercase with su lowercase SL
  2. Then count the numbers by comparing them to the original string.
  3. Class FindLetter3 {
  4. public static void Main (string[] args) {
  5. String str = "Helle, this is A test textfile.123456, Tannk you!!";
  6. String SU = Str.touppercase ();
  7. String SL = Str.tolowercase ();
  8. int lowcount = 0;
  9. int upcount = 0;
  10. int othercount = 0;
  11. For (int i = 0; i < str.length (); i++) {
  12. char charstr = Str.charat (i);
  13. char Charsu = Su.charat (i);
  14. char charsl = Sl.charat (i);
  15. //If it is not a letter and is a different character, it is counted directly with Othercount
  16. if (Character.isletter (CHARSTR)) {
  17. //If the original string is equal to the converted uppercase string, the original character is uppercase,
  18. //lowercase letters if they are equal to lowercase letters
  19. if (charstr = = Charsu) {
  20. upcount++;
  21. } Else if (charstr = = charsl) {
  22. lowcount++;
  23. }
  24. } Else {
  25. othercount++;
  26. }
  27. }
  28. System.out.println ("number of capital letters:" + upcount);
  29. System.out.println ("number of lowercase letters:" + lowcount);
  30. System.out.println ("number of other letters:" + othercount);
  31. }
  32. }


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.