Question

Assign to a variable in your program a triple-quoted string that contains your favourite paragraph of...

Assign to a variable in your program a triple-quoted string that contains your favourite paragraph of text — perhaps a poem, a speech, instructions to bake a cake, some inspirational verses, etc.

Write a function which removes all punctuation from the string, breaks the string into a list of words, and counts the number of words in your text that contain the letter “e”. Your program should print an analysis of the text like this:

Your text contains 243 words, of which 109 (44.8%) contain an "e".
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.List;
import java.util.ArrayList;
public class Main
{
// returns number of words in str
static int countWordsUsingSplit(String input) {
if (input == null || input.isEmpty()) {
return 0; }
String[] words = input.split("\\s+"); return words.length;
  
}
// Function to convert ArrayList<String> to String[]
static String[] GetStringArray(ArrayList<String> arr)
{
  
// declaration and initialise String Array
String str[] = new String[arr.size()];
  
// Convert ArrayList to object array
Object[] objArr = arr.toArray();
  
// Iterating and converting to String
int i = 0;
for (Object obj : objArr) {
str[i++] = (String)obj;
}
  
return str;
}

   public static void main(String[] args) {
String str = "I'm selfish, impatient and a little insecure. I make mistakes, I am out of control and at times hard to handle. But if you can't handle me at my worst, then you sure as hell don't deserve me at my best";
  
// similar to Matcher.replaceAll
str = str.replaceAll("\\p{Punct}","");
  
System.out.println(str);
System.out.println(countWordsUsingSplit(str));
ArrayList<String> wordArrayList = new ArrayList<String>();
for(String word : "I'm selfish, impatient and a little insecure. I make mistakes, I am out of control and at times hard to handle. But if you can't handle me at my worst, then you sure as hell don't deserve me at my best".split("\\s+")) {
wordArrayList.add(word);
}
int number_of_words_containing_e = 0;

String[] objects = GetStringArray(wordArrayList);
for(int i = 0;i<objects.length;i++){
char array[]=objects[i].toCharArray();
for(int j =0;j<array.length;j++){
if(array[j]=='e'){
number_of_words_containing_e = number_of_words_containing_e + 1;
break;
}
}
}

System.out.println(number_of_words_containing_e);
System.out.println("Your text contains "+countWordsUsingSplit(str)+" words, of which " +number_of_words_containing_e+" contain an 'e'.");
   }
}

Add a comment
Know the answer?
Add Answer to:
Assign to a variable in your program a triple-quoted string that contains your favourite paragraph of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT