Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

How to count certain characters in a word?

Asked by Stefan on 2 Apr 2013

I need to write a program, using loops and functions, that would count numbers of certain letters in a word.

For example if i input a word:'matlabprogram', and i want to count number of a,b,c,d letters in this word the program would say: a=3,b=3,c=0,d=0

Can you please help me?

P.S. sorry for my bad english

0 Comments

Stefan

Tags

No tags are associated with this question.

Products

No products are associated with this question.

2 Answers

Answer by Andrei Bobrov on 2 Apr 2013
Accepted answer
a = ('a':'d')'
b = 'matlabprogram'
out1 = [cellstr(a), num2cell(histc(b,a)')]

0 Comments

Andrei Bobrov
Answer by Azzi Abdelmalek on 2 Apr 2013
word='matlabprogram'
Lettre='abcd'
for k=1:numel(Lettre)
   m=Lettre(k);
   assignin('base',m,sum(ismember(word,m)))
end

1 Comment

Jan Simon on 2 Apr 2013

Although the OP asked for the names "a", "b", ... this is not a smart idea and I'd definitely avoid suggesting assignin.

Azzi Abdelmalek

Contact us