Counting how many times a word appears in a sentence

By Cooldudue | My Coding adventure | 10 May 2022


Hello everyone

This is a question I got in a coding interview and while I know how to solve it in python, Java is a different beast.  My approach was to split the sentence into an array of words and then check how many times a word appears in the array.  First, we have to remove all the punctuation marks. Then to convert the sentence into an array of words we slice the array from the start of the array to a space. Then one letter from the space till the next space. The problem then is we need to add a space at the end of the sentence to make sure we count the last word. From there we just compare each word and our function returns how many times a word occurs regardless of the case. This challenge definitely showed me that I need to revise some of the fundamental aspects of Java. Tomorrow I will start with sorting algorithms which is a section that I ignored since np.sort exists.

 

Thank you for reading and if you would like to see the code check out my GitHub.

How do you rate this article?


1

1


My Coding adventure
My Coding adventure

Hello, I am a recent graduate from the University of Cape Town. I have a Bachelor of Science in Mathematics and Applied Mathematics. I am interested in becoming a developer. If you are interested in hiring please check out my CV and we can get in contact. I will be posting any projects I am working on here. My Java is a little rusty so I will be working on small problems to familiarise myself again. My CV: https://docs.google.com/document/d/1qVs7YYqWrkn70xxv6EdDgAH3k7LVA2unwC8tLzQCS4M/edit?usp=sharing

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.