Write a function called howManyWordsAndSentences that takes in a string. Your fu
ID: 3749346 • Letter: W
Question
Write a function called howManyWordsAndSentences that takes in a string. Your function should return a dictionary where the keys are the order position of each SENTENCE (you may assume sentences will end with periods, exclamation points, or question marks). The data for each key should be a tuple containing the number of words in the sentence and a string containing the sentence itself (you should remove the extra spaces before or after each sentence). Your function should print out how many sentences there are in the input string. Ex: For the input string “Our class string is I Love Chocolate Milk. This may seem silly. But I really do love chocolate milk!” Your dictionary would have 3 entries The first key would be 1, and the data would be 8 (number of words) and the string “Our class string is I Love Chocolate Milk.” The second key would be 2, and that data would be 4 (number of words) and the string “This may seem silly”. The third key would be 3, and the data would be 7 (number of words) and the string “But I really do love chocolate milk!”Write a function called howManyWordsAndSentences that takes in a string. Your function should return a dictionary where the keys are the order position of each SENTENCE (you may assume sentences will end with periods, exclamation points, or question marks). The data for each key should be a tuple containing the number of words in the sentence and a string containing the sentence itself (you should remove the extra spaces before or after each sentence). Your function should print out how many sentences there are in the input string. Ex: For the input string “Our class string is I Love Chocolate Milk. This may seem silly. But I really do love chocolate milk!” Your dictionary would have 3 entries The first key would be 1, and the data would be 8 (number of words) and the string “Our class string is I Love Chocolate Milk.” The second key would be 2, and that data would be 4 (number of words) and the string “This may seem silly”. The third key would be 3, and the data would be 7 (number of words) and the string “But I really do love chocolate milk!”
Write a function called howManyWordsAndSentences that takes in a string. Your function should return a dictionary where the keys are the order position of each SENTENCE (you may assume sentences will end with periods, exclamation points, or question marks). The data for each key should be a tuple containing the number of words in the sentence and a string containing the sentence itself (you should remove the extra spaces before or after each sentence). Your function should print out how many sentences there are in the input string. Ex: For the input string “Our class string is I Love Chocolate Milk. This may seem silly. But I really do love chocolate milk!” Your dictionary would have 3 entries The first key would be 1, and the data would be 8 (number of words) and the string “Our class string is I Love Chocolate Milk.” The second key would be 2, and that data would be 4 (number of words) and the string “This may seem silly”. The third key would be 3, and the data would be 7 (number of words) and the string “But I really do love chocolate milk!”
Explanation / Answer
Code:
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Scanner;
public class CheggArray3 {
public static void main(String[] args) {
Dictionary<Integer,Integer> dict
=howManyWordsAndSentences("“Our class string is I Love Chocolate Milk.This may seem silly. But I really do love chocolate " +
"milk!");
int size=dict.size();
//Calculate size of dictionary
//it will give total number of sentence
System.out.println("Total sentence is :"+size);
for(int i=1;i<=size;i++){
int value=dict.get(i);
System.out.println("Sentence number "+i+" Have "+value +" words");
}
}
public static Dictionary<Integer,Integer> howManyWordsAndSentences(String str){
//Create one dictionary
System.out.println(str);
Dictionary<Integer,Integer> dict =new Hashtable<Integer,Integer>();
//Split on .
char ch='.';
String[] arr=str.split("[.]+");
//Start from 1
int cnt=1;
for(String s:arr){
//split on blank that means new word
String[] tmp=s.split(" ");
//insert it into dictionary
dict.put(cnt, tmp.length);
//increase key
cnt++;
}
return dict;
}
}
Output:
“Our class string is I Love Chocolate Milk. This may seem silly. But I really do love chocolate
milk!
Total sentence is :3
Sentence number 1 Have 8 words
Sentence number 2 Have 4 words
Sentence number 3 Have 7 words
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.