Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package counter;
- import java.util.Scanner;
- public class Counter
- {
- public static void main (String[] args)
- {
- String phrase = null; // a string of characters
- int countBlank; // the number of blanks (spaces) in the phrase
- int length, countA=0, countT=0, countS=0, countE=0; // the length of the phrase
- char ch; // an individual character in the string
- int i=0;
- Scanner scan = new Scanner(System.in);
- // Print a program header
- System.out.println ();
- System.out.println ("Character Counter");
- System.out.println ();
- // Read in a string and find its length
- System.out.print ("Enter a sentence or phrase (quit to stop): ");
- // Initialize counts
- countBlank = 0;
- // a for loop to go through the string character by character and count
- ch = ' ';
- // the blank spaces. Print the results
- String quit = null;
- while (!phrase.equals("quit"))
- {
- phrase = scan.nextLine();
- phrase = phrase.toLowerCase();
- length = phrase.length();
- for (i = 0; i < phrase.length(); i++)
- {
- ch = phrase.charAt(i);
- switch (ch)
- {
- case 'a':
- countA++;
- break;
- case 'e':
- countE++;
- break;
- case 's':
- countS++;
- break;
- case 't':
- countT++;
- break;
- case ' ': countBlank++;
- break;
- }
- }
- break;
- }
- System.out.println ("Number of blank spaces: " + countBlank);
- System.out.println ("Number of A's: " +countA);
- System.out.println("Number of E's: " +countE);
- System.out.println ("Number of T's: " + countT);
- System.out.println ("Number of S's: " + countS);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement