Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.69 KB | None | 0 0
  1. package syafi;
  2.  
  3. import java.util.Scanner;
  4. public class CountLetters {
  5.     public static void main(String[] args) {
  6.         Scanner input = new Scanner(System.in);
  7.        
  8.         System.out.print("Enter string: ");
  9.         String s = input.nextLine();
  10.        
  11.         int[] counts = countLetters(s.toLowerCase());
  12.         for(int i = 0; i < counts.length; i++) {
  13.             if (counts[i] != 0)
  14.                 System.out.println((char)('a' + i) + " appears " + counts[i] + ((counts[i] == 1) ? " time" : " times"));
  15.         }  
  16.     }
  17.     public static int[] countLetters(String s) {
  18.         int[] counts = new int[26];
  19.        
  20.         for (int i = 0; i < s.length(); i++) {
  21.             if(Character.isLetter(s.charAt(i)))
  22.                 counts[s.charAt(i) - 'a']++;
  23.     }
  24.         return counts;
  25.     }
  26.  
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement