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
Question 83 :Which type of fiber optic connector connects to a terminating device by pushing the connector into the terminating
STALIN [3.7K]

The Lucent connector(LC) is a type of fiber optic connector used here.

Explanation:

A Lucent connector (LC) is a type of fiber optic connector connects to a terminating device by pushing the connector into the terminating device and can be removed by depressing the tab on the connector and pulling it out of the terminating device.

It is a standard ceramic Ferrule connector and has good performance and is favored for single mode.

LC is a latched connector which have same handling capacities like SC. They have small flange on top similar to an RJ-45 connector that aids secure connection. It has a smaller connector housing and a smaller 1.25 mm ferrule.

3 0
3 years ago
Create a program which will input data into a pipe one character at a time. Count the number of characters as they are written i
LenKa [72]

Answer:

a)

#include<stdio.h>

#include<stdlib.h>

#include<fcntl.h>

#include<unistd.h>

 

#define BUFSIZE 16

int main(){

   //pipe descriptors

   char msg[BUFSIZE];

   char buf[BUFSIZE];

   int pipefd[2];

   if(pipe(pipefd) == -1){

       //pipe creation error

       perror("pipe");

       exit(EXIT_FAILURE);

   }

   //pipe creation successfull

   //write four messages

   sprintf(msg , "apple");

   write(pipefd[1], msg, BUFSIZE);

   printf("Written %s\n", msg);

   sprintf(msg , "boy");

   write(pipefd[1], msg, BUFSIZE);

   printf("Written %s\n", msg);

   sprintf(msg , "cat");

   write(pipefd[1], msg, BUFSIZE);

   printf("Written %s\n", msg);

   sprintf(msg , "dog");

   write(pipefd[1], msg, BUFSIZE);

   printf("Written %s\n", msg);

   //read

   read(pipefd[0], buf, BUFSIZE);

   printf("Read: %s\n", buf);

   read(pipefd[0], buf, BUFSIZE);

   printf("Read: %s\n", buf);

   read(pipefd[0], buf, BUFSIZE);

   printf("Read: %s\n", buf);

   read(pipefd[0], buf, BUFSIZE);

   printf("Read: %s\n", buf);

   close(pipefd[0]);

   close(pipefd[1]);

   return 0;

}

(b)

#include<stdio.h>

#include<stdlib.h>

#include<fcntl.h>

#include<unistd.h>

#include<wait.h>

#define BUFSIZE 16

int main(){

   //pipe descriptors

   char msg[BUFSIZE];

   char buf[BUFSIZE];

   int pipefd[2];

   if(pipe(pipefd) == -1){

       //pipe creation error

       perror("pipe");

       exit(EXIT_FAILURE);

   }

   //pipe creation successfull

   //write four messages

   if(fork() == 0){

       //this is child

       //close unused write end

       close(pipefd[1]);

       read(pipefd[0], buf, BUFSIZE);

       printf("This is child process reading first message. Content is %s\n", buf);

       read(pipefd[0], buf, BUFSIZE);

       printf("This is child process reading second message. Content is %s\n", buf);

       read(pipefd[0], buf, BUFSIZE);

       printf("This is child process reading third message. Content is %s\n", buf);

       read(pipefd[0], buf, BUFSIZE);

       printf("This is child process reading fourth message. Content is %s\n", buf);

       close(pipefd[0]);

       //exit from child

       exit(EXIT_SUCCESS);

   }

   //this is parent process

   //close unused read end

   close(pipefd[0]);

   sprintf(msg , "apple");

   printf("This is parent process. Writing first message into pipe\n");

   write(pipefd[1], msg, BUFSIZE);

   sprintf(msg , "boy");

   printf("This is parent process. Writing second message into pipe\n");

   write(pipefd[1], msg, BUFSIZE);

   sprintf(msg , "cat");

   printf("This is parent process. Writing third message into pipe\n");

   write(pipefd[1], msg, BUFSIZE);

   sprintf(msg , "dog");

   printf("This is parent process. Writing fourth message into pipe\n");

   write(pipefd[1], msg, BUFSIZE);

   close(pipefd[1]);

   //wait for child

   wait(NULL);

   return 0;

}

(c)

#include<stdio.h>

#include<stdlib.h>

#include<fcntl.h>

#include<unistd.h>

#include<sys/wait.h>

#include<sys/types.h>

#include<signal.h>

typedef struct sigaction Sigaction;

unsigned long long size = 0;

void alarmhandler(int sig){

   //alarm fired writing blocked

   printf("Write blocked after %llu characters\n", size);

   exit(EXIT_SUCCESS);

}

int main(){

   //pipe descriptors

   int pipefd[2];

   if(pipe(pipefd) == -1){

       //pipe creation error

       perror("pipe");

       exit(EXIT_FAILURE);

   }

   //install handler

   sigset_t mask , prev;

   sigemptyset(&mask);

   sigaddset(&mask , SIGALRM);

   sigprocmask(SIG_BLOCK , &mask , &prev);

   Sigaction new_action;

   sigemptyset(&new_action.sa_mask);

   new_action.sa_flags = SA_RESTART;

   new_action.sa_handler = alarmhandler;

   sigaction(SIGALRM , &new_action , NULL);

   sigprocmask(SIG_SETMASK , &prev, NULL);

   while(1){

     

       //print size on multiple of 4

       if(size != 0 && size % 1024 == 0){

           printf("%llu characters in pipe\n", size);

       }

       //reset previous alarm

       alarm(0);

       //set new alarm for 5 seconds

       alarm(5);

       //write to pipe one character

       write(pipefd[1], "A", sizeof(char));

       size++;

   }

   return 0;

}

Explanation:

Output for a, b, c are pasted accordingly

4 0
3 years ago
Which option correctly identifies the expense that will cost you the most when you are living on your own?
larisa86 [58]
Cccccccccccccccccccccc
5 0
3 years ago
+20 POINTS!!!~~~~~When adding delegates to his mailbox, which role should Joel use if he would like the user to be able to read
DochEvi [55]

Answer:

b on edge

Explanation:

4 0
2 years ago
Which of the following is least likely to be a scientific experiment
Dennis_Churaev [7]

Answer:

what causes gravity to have a amount of force on one object but not another

Explanation:

3 0
3 years ago
Other questions:
  • The read/write heads of a hard disk gently rest on the hard disk platters in order to read and write the data.
    13·1 answer
  • What term describes data actions being performed by endpoint devices, such as printing a report from a desktop computer?
    7·1 answer
  • Did this technological advancement take away or create jobs? Overall, what was the contribution of this technological advancemen
    12·1 answer
  • What is the main reason that IT managers believe the future IT career model will be diamond-shaped, with few entry-level IT jobs
    7·1 answer
  • Briefly describe the client/server model.
    11·1 answer
  • Short Essay on the history of Computer​
    6·2 answers
  • After separating from Mexico, Texas passed laws that made it harder for slave owners to _____________ slaves.
    12·2 answers
  • Please help. 10 points
    5·2 answers
  • How did hitles rules in nazi germany exemplify totiltarian rule?
    10·1 answer
  • What function is used to return the results in column D?
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!