Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package syafi;
- import java.util.Scanner;
- public class CountLetters {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- System.out.print("Enter string: ");
- String s = input.nextLine();
- int[] counts = countLetters(s.toLowerCase());
- for(int i = 0; i < counts.length; i++) {
- if (counts[i] != 0)
- System.out.println((char)('a' + i) + " appears " + counts[i] + ((counts[i] == 1) ? " time" : " times"));
- }
- }
- public static int[] countLetters(String s) {
- int[] counts = new int[26];
- for (int i = 0; i < s.length(); i++) {
- if(Character.isLetter(s.charAt(i)))
- counts[s.charAt(i) - 'a']++;
- }
- return counts;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement