Answer:
Here is the JAVA program:
import java.util.*; //to use Scanner class, Lists
public class Main{ //name of class
public static void main(String[] args) { //start of main function
String sentence = "CaT and catalog are not same"; //a string
sentence = sentence.toLowerCase(); // converts the words in the sentence to lowercase
Scanner sc = new Scanner(sentence); //creates Scanner class object
List<String> profane_words = profaneList(); // creates a list of profane words i.e. cat, dog and llama
int count = 0; //count the number of profane words
while (sc.hasNext()) { //reads string as tokens and returns true if scanner has another token
String word = sc.next(); //returns the next complete token (word) and store it into word variable
for (String profane : profane_words) { // looks for each profane in profane_words list
if (word.matches(".*\\b" + profane + "\\b.*") && ! sentence.matches(".*" + profane + "[a-z].*\\b" + profane + "\\b.*") && ! sentence.matches(".*[a-z]" + profane + ".*\\b" + profane + "\\b.*")) { //uses matches() method that identifies whether a word in the string matches the given regular expression.
count++; //each time if the word of sentence matches with word given in profane_list then 1 is added to count
System.out.println("The word " + profane + " is a profane!"); //displays the word which is profane
} } }
System.out.println("Number of profane words: "+ count); } //displays the number of profane words found in the sentence
private static List<String> profaneList() { //list of profane words
List<String> profane_words = new ArrayList<>(); //creates an array list of profane words named as profane_words
profane_words.add("dog"); //adds word "dog" to the list of profane words
profane_words.add("cat"); //adds word "cat" to the list of profane words
profane_words.add("llama"); //adds word "llama" to the list of profane words
return profane_words; }} //returns the list of profane words
Explanation:
I will explain the program with an example:
sentence = "CaT and catalog are not same";
First this sentence is converted to lower case as:
sentence = "cat and catalog are not same";
Now the while loop uses hasNext() which keeps returning the next token and that token is stored in word variable.
Next the for loop iterates through the list of profane_words to check each profane word with the word in a sentence.
Next this statement:
if (word.matches(".*\\b" + profane + "\\b.*") && ! sentence.matches(".*" + profane + "[a-z].*\\b" + profane + "\\b.*") && ! sentence.matches(".*[a-z]" + profane + ".*\\b" + profane + "\\b.*"))
uses regular expressions to find the profane words in the sentence. The logical operator&& AND between the three expressions is used so all of the three must hold for the if condition to evaluate to true.
word.matches(".*\\b" + profane + "\\b.*") expression uses \b to check an exact match of the word. \b is meta-character to find a match at the beginning and at the end. So this checks if the word of a sentence matches with the profane in the profane list.
! sentence.matches(".*" + profane + "[a-z].*\\b" + profane + "\\b.*") && ! sentence.matches(".*[a-z]" + profane + ".*\\b" + profane + "\\b.*"))
These two expressions check if profane does not include as a part of another word in the sentence. This solves the extra challenge. Like here in the sentence we have a word catalog which is not a profane but a profane cat is included in this. So this will not be considered as a profane.
Every time a word matches with the profane from profane list, the value of count variable is incremented to 1.
In the end the statement:
System.out.println("Number of profane words: "+ count);
prints the number of profane words in the sentence.
List<String> profane_words = new ArrayList<>(); creates the list of profane words and add() method adds the words to the list profane_words. So profane_words list contain "cat", "dog" and "llama"
Hence the output of this program is:
The word cat is a profane! Number of profane words: 1
The program output is attached in screenshot