Question

In: Computer Science

Instructions Today, all input and output has been taken care of for you. All you need...

Instructions

Today, all input and output has been taken care of for you. All you need to do is finish off the function wordCount that calculates the number of words in a string.

Function Details

Input

  • Input has been handled for you. A string will be read in.

Processing

  • Complete the wordCount function. Note that you have been given Java comments describing its expected parameters and return value.
/**
 * wordCount (String) -> int
 * Calculates the number of words in a given string
 * @param ?? (str): String of words
 * @return Number of words in the string (int)
 */
  • The method takes in a string. It then calculates the the number of words in that string and returns that value.

    HINT: This can be done by first splitting the string into individual words using the String.split() method. Then you just need to count how many objects the array holds (and there is an array method for that as well!

Output

  • Output has also been taken care of for you.

Sample input/output:

use JAVA method complete this question

Input Output
I have really enjoyed teaching this class and all of you!

Word count: 11

I am designing a test and do not want to get bogged down in what the text actually says.

Word count: 19

Solutions

Expert Solution

Program

filename: WordCount.java

public class WordCount{

/**
* wordCount (String) -> int
* Calculates the number of words in a given string
* @param inputString String of words
* @return Number of words in the string (int)
*/
public static int wordCount(String inputString) {
int count = 0;
int inWord = 0;
for (char letter : inputString.toCharArray()) {
  
//non-space means were "in a word"
if (letter != ' ') {
inWord = 1;
} else {
// space is the delimiter, but we only want to count a word if
// we were previously "in a word"
if (inWord == 1) {
count++;
inWord = 0;
}
}
}
// Just in case we were "in a word," but never encountered another space
if (inWord == 1) {
count++;
}
return count;
}

public static void main(String args[]){
String input = "I have really enjoyed teaching this class and all of you!";
System.out.println("wordCount(\"I have really enjoyed teaching this class and all of you!\") : " + wordCount(input));
}
}

Output

==========================================================================================

Hope this helps!

Please let me know if any changes needed.

Thank you!

I hope you're safe during the pandemic.


Related Solutions

In this assignment, you need to demonstrate your ability in using input, output, data types, and...
In this assignment, you need to demonstrate your ability in using input, output, data types, and if statement in C++ program. Assume that you need write a C++ program for a cash register. There are only four items in the store: Cereal, $3.99 Milk, $3.99 Egg, $0.25 Water, $ 1.50 Once a customer purchases items, you will ask her/his how many of them are bought. The quantity can be in the range of 0-10 (including 0 and 10). Then, calculate...
You can use any language. You have to provide all the data, output and input. You...
You can use any language. You have to provide all the data, output and input. You are to use Linked Lists to do this Program. The XYZ Widget store receives shipments of widgets at various costs. The store’s policy is to charge a 30% markup, and to sell widgets which were received earlier before widgets which were received later. This is called a FIFO policy. Write a program using linked lists that reads in three types of input data and...
PLEASE READ ALL OF THESE INSTRUCTIONS BEFORE BEGINNING THIS ASSIGNMENT. For this assignment, you need to...
PLEASE READ ALL OF THESE INSTRUCTIONS BEFORE BEGINNING THIS ASSIGNMENT. For this assignment, you need to analyze the information below from BOTH the management AND the employee perspective. This information pertains to a labor union in a simulated/made up/not real firm in Glen Ellyn. The first part of your information relates to Management – the second part relates to the Labor Union employees. I have provided you with information from the last union negotiations at the plant in 2016. It...
****NEED CODED IN C++, READ THE INSTRUCTIONS CAREFULLY AND PAY ATTENTION TO THE INPUT FILE, IT...
****NEED CODED IN C++, READ THE INSTRUCTIONS CAREFULLY AND PAY ATTENTION TO THE INPUT FILE, IT IS REQUIRED FOR USE IN THE PROBLEM**** You are to generate a list of customers to serve based on the customer’s priority, i.e. create a priority queue/list for a local company. The company has been receiving request and the request are recorded in a file, in the order the request was made. The company processes each user based on their priority, the highest priority...
there are files called LinkedList.h, and Array.h and all the functionality you need has already been...
there are files called LinkedList.h, and Array.h and all the functionality you need has already been implemented. There is also a file called TimeSupport.h that allows you to measure the running time of code segments. There is also a file called RandomSupport.h, which you can use to generate good random numbers. In your app.cpp , create a demo illustrating a scenario where storing numbers in a linked list is more efficient than an array. Your demo should generate a sufficiently...
"It has been contended that all contracts must be agreements but not all agreements need to...
"It has been contended that all contracts must be agreements but not all agreements need to be contracts" Comment on the validity of this statement. Support with references.
Need this program in python. The data must be taken from user as input. Write a...
Need this program in python. The data must be taken from user as input. Write a program that prompts the user to select either Miles-to-Kilometers or Kilometers-to-Miles, then asks the user to enter the distance they wish to convert. The conversion formula is: Miles = Kilometers X 0.6214 Kilometers = Miles / 0.6214 Write two functions that each accept a distance as an argument, one that converts from Miles-to-Kilometers and another that converts from Kilometers-to-Miles The conversion MUST be done...
This question need to be solved using java coading :- I. Input All input data are...
This question need to be solved using java coading :- I. Input All input data are from a file "in.dat". The file contains a sequence of infix form expressions, one per line. The character '$' is an end mark. For example, the following file has four infix form expressions: 1 + 2 * 3 ^ ! ( 4 == 5 ) $ 3 * ( 6 - 4 * 5 + 1) + 2 ^ 2 ^ 3 $ 77...
This question need to be solved using java coading :- I. Input All input data are...
This question need to be solved using java coading :- I. Input All input data are from a file "in.dat". The file contains a sequence of infix form expressions, one per line. The character '$' is an end mark. For example, the following file has four infix form expressions: 1 + 2 * 3 ^ ! ( 4 == 5 ) $ 3 * ( 6 - 4 * 5 + 1) + 2 ^ 2 ^ 3 $ 77...
Read the input one line at a time until you have read all lines. Now output...
Read the input one line at a time until you have read all lines. Now output these lines in the opposite order from which they were read.. import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; public class Part12 {       /**    * Your code goes here - see Part0 for an example    * @param r the reader to read from    * @param w the writer to write to    * @throws IOException...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT