Problem 43670. Words Count: A String Array Approach

Given an input character vector consisting of words, punctuation marks, white spaces, and possibly newline characters (\n), arrange the unique words alphabetically in a string array and calculate the histogram count of every unique word.


  1. Case is insensitive, e.g., WORDS and words are treated as the same word, and you may return in the output string array either the uppercase or lowercase.
  2. Punctuation marks are limited only to comma (,), period (.), colon (:), semi-colon (;), question mark (?), and exclamation mark (!).

For example, given the input txt as a character vector,

txt = 'I love MATLAB and Cody, but I don''t like trivial matlab problems on cody.';

the outputs should be

words = string({'and';'but';'Cody';'don''t';'I';'like';'love';'MATLAB';...
count = [1; 1; 2; 1; 2; 1; 1; 2; 1; 1; 1];

Hint: The R2016b documentation provides a good example of text data analysis via the string array approach. However, some steps illustrated in that example are unnecessary, and we can indeed accomplish the same task in a simpler way.

Related problems in this series:

Solution Stats

26.91% Correct | 73.09% Incorrect
Last solution submitted on Jun 03, 2019

Problem Comments

Solution Comments

Problem Recent Solvers45

Suggested Problems

More from this Author43

Problem Tags