A text analyzer
The problem
Build a program that reads lines of text and reports the number of lines, words and characters, the most common words and the longest word.
Requirements
- Read every line until the input ends and clean it with trim and strtolower
- Split words with explode and remove punctuation with str_replace
- Count words in an associative array and sort it by count, then by word
- Write small functions for each report so each one can be tested alone
Technologies
- PHP 8.4
- Strings
- Arrays
- Functions
- Closures
Step by step
- Read all the lines into an array first and print the line count.
- Write a function that turns a line into clean lower case words.
- Count the words with the ?? 0 pattern and sort the counts.
- Print the top words and the longest word with printf.
Expected result
A tool that prints a clear text report for any input text.
How it is evaluated
- Counts are correct for punctuation and capital letters
- Ties are sorted the same way every time
- Functions are small and named with verbs
- Empty input does not cause an error
Ways to improve it
- Ignore very common words such as the and a
- Add a histogram of word lengths
- Read a second input as a list of words to find