82.0k views
0 votes
Write a Console Java program that asks the user to enter one sentence on the keyboard. Output on the console:

1) The number of words in the sentence
2) A count of each alphabet in the sentence in alphabetical order (ignore zero counts, ignore Upper/Lowercase difference. That is, count 'A' and 'a' as the same) . Assume that the user will enter a sentence with alphabets. No need to validate user entry. Example: Sentence: This is a great Fall Semester Number of words: 6 a-3, e-4. f-1, g-1, h-1, i-2, l-2, m-1, r-2 , s-4, t-3

User Zanderi
by
4.3k points

1 Answer

4 votes

Answer:

Here is the JAVA program:

import java.util.Scanner; //to accept input from user

public class Main{ //class name

public static void main(String[] args){ //start of main function

String sentence; //to store the input sentence

int i, length; //declare variables

int count[] = new int[256]; //creates a counter array

Scanner input = new Scanner(System.in); //creates Scanner class object to accept input from user

System.out.println("Enter a sentence: "); //prompts user to enter a sentence

sentence = input.nextLine(); //scans and reads the entire sentence string from user

String[] words = sentence.split("\\s+"); //splits the sentence into an array with separator as one or more white spaces and stores this array in words

System.out.println("Number of words: " + words.length); //display the number of words in sentence by using length which returns the length of the words array

sentence = sentence.toLowerCase(); // converts the sentence into lower case to ignore case sensitivity

sentence = sentence.replaceAll("\\s+",""); //removes the empty spaces from sentence

length = sentence.length(); //stores the length of the sentence string into length variable

for (i = 0; i < length; i++) { //iterates through the length of the sentence

count[(int) sentence.charAt(i)]++; } //counts occurrence of each alphabet in the sentence and stores it in count array

for (i = 0; i < 256; i++) { displays occurrence of alphabet in sentence

if (count[i] != 0) { //if count array is not empty

System.out.println((char) i + "-" + count[i]); }}}}

//prints each alphabet and its count

Step-by-step explanation:

The program prompts the user to enter a sentence. Suppose the user enters "We won" so

sentence = "We won"

Now this sentence splits into an array with white space as separator and stored in words array. So words array has two words of sentence now i.e. "We" and "won"

Next words.length is used to return the length of words array. So length of words array is 2 hence

number of words = 2

Next the sentence is converted to lower case and spaces are removes so sentence becomes:

wewon

sentence.length() returns the length of sentence which is 5.

Next the for loop is used. At each iteration of this loop occurrence each character of sentence is added to the count[] array. charAt() is used to return a character at each index position of string. So the frequency of every character is counted and stored in count array.

Next is a for loop that is used to display each alphabet in the sentence along with its frequency in alphabetic order.

Hence the entire program gives the output:

Number of words: 2 e-1 n-1 o-1 w-2

The screenshot of the output of example given in the question is attached.

Write a Console Java program that asks the user to enter one sentence on the keyboard-example-1
User Warbo
by
5.2k points