1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
astra-53 [7]
3 years ago
6

Write a Console Java program that asks the user to enter one sentence on the keyboard. Output on the console:

Computers and Technology
1 answer:
Nady [450]3 years ago
6 0

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

           

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.

You might be interested in
For this exam review lab exercise, you will develop a program that fixes faulty data in a one-dimensional array of integers. Fau
Paladinen [302]

Answer:

start with what you know

Explanation:

3 0
3 years ago
Write a program with total change amount as an integer input that outputs the change using the fewest coins, one coin type per l
alexandr1967 [171]

Answer:

a=input("Amount in pennies")

b=int(a)

dollars=0

dimes= 0

quarters=0

nickels=0

pennies = 0

dollars = int(b/100)

b= b- dollars *100

quarters=int(b/25)

b=b-quarters*25

dimes = int(b/10)

b = b -dimes*10

nickels=int(b/5)

b=b - nickels * 5

pennies = b

print(dollars)

print(dimes)

print(nickels)

print(pennies)

Explanation:

The required program is in answer section. Note, the amount is entered in pennies.

7 0
3 years ago
Write a program in C++ to implement bubblesort using the swap function ?
marishachu [46]

C++ program for implementation of Bubble sort  

#include <bits/stdc++.h>  

using namespace std;  

 void swap(int *x, int *y)  /*Defining Swap function of void return type*/

{  

  int temp = *x;  //Swaping the values  

   *x = *y;  

   *y = temp;  

}  

void bubbleSort(int array[], int n) /*Defining function to implement bubbleSort  */

{  

  int i, j;  

  for (i = 0; i < n-1; i++)      

      for (j = 0; j < n-i-1; j++)  /*The last i components are already in location  */

      if (array[j] > array[j+1])  

          swap(&array[j], &array[j+1]);  //Calling swap function

}  

int main()  //driver function

{  

   int array[] = {3, 16, 7, 2, 56, 67, 8}; //Input array  

   int n = sizeof(array)/sizeof(array[0]);  //Finding size of array

   bubbleSort(array, n); //Function calling  

   cout<<"Sorted array: \n";  

   for (int i = 0; i < n; i++)  //printing the sorted array

       cout << array[i] << " ";  

   cout << endl;  

 return 0;  

}  

<u>Output</u>

Sorted array:  

2 3 7 8 16 56 67

3 0
3 years ago
P16 Consider the following network. ISP B provides national backbone service to regional ISP A. ISP C provides national backbone
Art [367]

Solution :

Considering the following data :

$\text{ISP B}$ provides the national backbone to the regional $\text{ISP A}$.

$\text{ISP C}$ provides the national backbone to the regional $\text{ISP D}$.

Each of the ISP consists of one autonomous systems (AS).

$\text{ISP B}$ would like to hand over the traffic of C through the $\text{West}$ Coast.

The $\text{ISP C}$ would like to receive traffic from B's peering point via East Coast.

One way for $C$ to force $B$ to hand over all of $B$’s traffic to $D$ on the east coast is for $C$ to  only advertise its route to $D$ via its east coast peering point with $C$.

7 0
2 years ago
Explain cell reference and its types with examples?​
skad [1K]

Answer:

There are two types of cell references: relative and absolute. Relative and absolute references behave differently when copied and filled to other cells. Relative references change when a formula is copied to another cell. Absolute references, on the other hand, remain constant, no matter where they are copied.

8 0
2 years ago
Other questions:
  • A(n) ____ loop executes a predetermined number of times.
    12·1 answer
  • Write a program that can be used to gather statistical data about the number of movies college students see in a month. The prog
    11·1 answer
  • Your friend sends you a photograph of his son. you want to enlarge the photograph. which program type will you use to make this
    15·1 answer
  • 2. What type of expansion card allows your computer to
    12·1 answer
  • What are three ways you cite evedince
    5·2 answers
  • What is a main cause of an aurora?
    8·1 answer
  • 14. In cell B14, create a formula without using a function that adds 1 to the value in cell B12 and then multiplies the result b
    9·1 answer
  • It is a minor cereal of importance only in West Africa where it is eaten in place of rice during during famines. Used for salads
    10·1 answer
  • it is good to know and use the npsd framework while solution envisioning as part of the value discovery cycle. What is NPSD?
    10·1 answer
  • write a function named get majority last name that accepts as its parameter a dictionary from strings to strings the keys of the
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!