Java Programming: Enter a string of lowercase strings to count the occurrences of each letter

Source: Internet
Author: User

* Requirement: Count each letter in the string:
* Description: Write a program prompting the user to enter a string,
* Then count the number of occurrences of each letter in the string, ignoring the case of the letter.
*
Principle
* 1. Use the toLowerCase () method in the string class to convert uppercase letters in strings to lowercase.
* 2. Constructs an array with 26 int worth of CH, and each element records the number of occurrences of a letter.
* i.e., ch[0] records the number of a, ch[1] the number of records B.
* 3. For each character in the character, determine if it is a lowercase letter, and if so, the corresponding counter in the array is incremented by 1.

The first style of source code:

Package fourth day; Import Java.util.scanner;public class statistic character {public static void main (string[] args) {Scanner sc = new Scanner (S ystem.in); int[] A = new int[26];//array holds the number of occurrences of the corresponding 26 letters, such as a[0], the number of occurrences of the letter A, the value of a[2] corresponds to the number of occurrences of c ... System.out.println ("Please enter a string of lowercase strings"); String str=sc.nextline (); Str=str.trim (). toLowerCase ();//Remove front and back spaces and turn all lowercase letters//This for loop to find the number of occurrences of each letter for (int i = 0; i < Str.length (); i++) {char c = str.charat (i);//Take out each letter int index=c-' a ';//This allows you to get an array of each letter corresponding to the subscript a[index]=a[index]+1;//corresponding letters appear the array of letters and 1}/ /This for loop prints the number of occurrences of each letter and does not print output for (int i = 0; i < a.length; i++) {if (a[i]!=0)//equals 0 The letter does not appear and there is no need to print {SYSTEM.OUT.PRINTLN ("Letter" + (char) (i+ ' a ') + "appears:" +a[i]+ ");}}}
the first style of source code:To wrap the number of statistical letters into a static method

Package  The third day _ exercises; Import Java.util.scanner;public class statistics number of characters {//static method counts the number of occurrences of each letter public static int[] Countletters ( String str) {int[] a = new int[26];//this for loop to find the number of occurrences of each letter for (int i = 0; i < str.length (); i++) {char c = str.charat (i);// Take out each letter int index = c-' a ';//So you can get the array subscript for each letter A[index] = A[index] + 1;//The corresponding letter appears, the array of letters is stored plus 1}return A;} public static void Main (string[] args) {Scanner sc = new Scanner (system.in); String str = sc.nextline (); int[] A = countletters (str);//Call the function that counts the occurrences of each letter, the return type is an array//this for loop prints the number of occurrences of each letter, and does not print output for (int i = 0; i < a.length; i++) {if (a[i]!=0)//equals 0 equivalent to this letter does not appear there is no need to print {System.out.println ("letter" + (char) (i+ ' a ') + "appears:" +a[i]+ "Times");}}}


Java Programming: Enter a string of lowercase strings to count the occurrences of each letter

Related Article

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.