# Count word except some special case

1 view (last 30 days)
Mai Thành on 8 Dec 2021
Commented: Chunru on 10 Dec 2021
Count the number of words in a string, excluding the article (a, an, the), regardless of capitalization. Demonstrate the working of your code with an example string.
##### 2 CommentsShow 1 older commentHide 1 older comment
Mai Thành on 9 Dec 2021
Oh I don't know about that, I'm sorry :(
so this is my code so far
A1=strfind(x,'A ');
A2=strfind(x,' A.');
A3=strfind(x,' A ');
real_A= A1+A2-A3;
a1=strfind(x,'a ');
a2=strfind(x,' a.');
a3=strfind(x,' a ');
real_a= a1+a2-a3;
an1=strfind(x,'an ');
an2=strfind(x,' an.');
an3=strfind(x,' an ');
real_an=an1+an2-an3;
An1=strfind(x,'An ');
An2=strfind(x,' An.');
An3=strfind(x,' An ');
real_An=An1+An2-An3;
the1=strfind(x,'the ');
the2=strfind(x,' the.');
the3=strfind(x,' the ');
real_the=the1+the2+the3;
The1=strfind(x,'The ');
The2=strfind(x,' The.');
The3=strfind(x,' The ');
real_The=The1+The2+The3;
real=real_A + real_a + real_an + real_An + real_the + real_The;
pos=strfind(x, ' ');
y=numel(pos)+1-real;
fprintf ('The number of word in the string is %f\n', y)
I calculate the number of spaces in the sentence and add 1 into it to have the number of words. Then I calculate the number of article words (a, an, the) and use the total words minus article words to have the answer. However, the are some case that ans =[] and any number add or minus with ans will have the result is []. I don't know how to fix it :(

Chunru on 9 Dec 2021
x ='some text here with a and an and the';
% you can use "lower" to convert it into lower case
%
% consider "split" to split the string into words with appropriate
% delimiters
%
% consider "ismember" to test the split words belong to "a" "an" and "the"
% idx = ~ismember(splitwords, {'a', 'an', 'the'})
% "sum" up idx to get the number of words
% n = sum(idx)
Chunru on 10 Dec 2021
x = 'A thesis is an example of The Program';
x = lower(x)
x = 'a thesis is an example of the program'
splitwords = split(x, {' ', '.', ',', ':', '?', '!'})
splitwords = 8×1 cell array
{'a' } {'thesis' } {'is' } {'an' } {'example'} {'of' } {'the' } {'program'}

### Categories

Find more on Characters and Strings in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!