Question 2
Write a program to accept a paragraph containing TWO
sentences only. The sentences may be terminated by either ‘.’, ‘?’ or ‘!’ only.
Any other character may be ignored. The words are to be separated by a single
blank space and must be in UPPER CASE.
Perform the following tasks:
(a)
Check for the validity of the accepted paragraph
for the number of sentences and for the terminating character.
(b)
Separate the two sentences from the paragraph
and find the common words in the two sentences with their frequency of occurrence
in the paragraph.
(c)
Display both the sentences separately along with
the common words and their frequency, in the format given below:
Example 1
INPUT: IS IT RAINING? YOU MAY GET
WET IF IT IS RAINING.
OUTPUT: IS IT RAINING?
YOU
MAY GET WET IF IT IS RAINING.
COMMOM
WORDS FREQUENCY
IS 2
IT 2
RAINING 2
Example 2
INPUT: INDIA IS MY MOTHERLAND AND
I AM PROUD OF MY MOTHERLAND. ALL INDIANS ARE MY BROTHERS AND SISTERS.
OUTPUT: INDIA IS MY MOTHERLAND AND I AM PROUD OF MY
MOTHERLAND.
ALL
INDIANS ARE MY BROTHERS AND SISTERS.
COMMOM
WORDS FREQUENCY
MY 3
AND 2
Example 3
INPUT: ARE YOU COMING? I AM
GETTING LATE.
OUTPUT: ARE YOU COMING?
I
AM GETTING LATE.
NO
COMMON WORDS
Example 4
INPUT: AT LAST, THE TIGER WAS
SAVED.
OUTPUT: INVALID INPUT
SUMMMARY
I will definitely recommend not attempting this type of questions in your final exam. Below are few reasons to avoid problems based on Strings in general.
1. Numbers or Matrix problems are easy to handle, less cumbersome.
2. Logic for problems related to string are generally complicated and more complicated to implement in code.
3. More prone to error while coding.
4. Lengthy problems.
5. Testing need to be done very carefully with lots of valid input.
Now coming back to this problem, it is very unique and somewhat much lengthy compared to other "string problems" in various years.
I will discuss here the main logic of the program step by step.
In the main program I have taken the input and checked its validity accordingly. It is easy to understand and I am leaving that part.
Moving forward, the input contains two sentences which need to be separated and displayed. For this I have used the function public void sentenceExtractor() it splits the string in two sentences.
The two sentences are stored in SentenceOne & sentenceTwo respectively.
After thet the logic is quite simple!( implementation is hard!)
Extract the words of each of the sentences ( achieved using the private String[] wordExtractor(String msgTemp) function, used in many programs on this blog!) and store it somewhere.
Now notice that the number of common words cannot exceed the number of words in the first senctence. For example,
INPUT: vi
Once you extract the words and store somewhere ( THIS, IS, MY, BLOG, ON, ISC, COMPUTER, PRACTICALS) and (PLEASE, SHARE, THIS, BLOG).
Now we need to compare this too String arrays by taking the first element (THIS) from the first array and compare it to all the element in the second array. This will determine whether or not the word is common in both the sentences. This we have to do for each of the words in the first array. Obviously if there is some word which is present in the second sentence but not in the first one cannot be common!
Now once we collect all the common words, we need to find its frequency in the original sentence and display it!
But there will be one problem! it will cause repetition!
For example, if the input is IS IS RAINING? YOU MAY GET WET IF IT IS RAINING.
when you extract the words (IS,IS,RAINING) you don't know that IS is being repeated twice and is common! So you have to solve this issue too. Again its not that difficult but definitely time consuming. Below is the piece of the code in which to avoid duplicate printing, I have used a flag "duplicateflag" to check whether the common word has already printed or not!
Another problem that you can face based on the technique of word extraction. As the words are separeted by a single space, we will be using that single space divider to extract the words. So if the input is like this: HEY THERE, ARE YOU PRACTISING ENOUGH! THERE AREN'T ENOUGH GOOD BLOGS.
The extracted words will be like (HEY, THERE, , ARE, YOU......) Now when you match with the words of the second sentence, you won't be getting THERE as a common word. Because you are comparing THERE, with THERE! So you need to be careful and check for this:
Go through the code and try to understand the methods seperately! If you have any question comment below.
No comments:
Post a Comment