MATLAB Answers


Finding the number of distinct letters in a string

Asked by Andrew
on 19 Nov 2011
Latest activity Commented on by Dylan Lalonde
on 14 Oct 2015

I'm trying to write a function that computes the number of occurrences of each letter in the string, and also the number of occurrences of adjacent pairs of characters in the string. I'm pretty stuck. Say if my input was a string, 'calculatorcalculator'...I'm trying to have my output show up something like

Number of c = 2
Number of a = 4
Number of l = 4 
Number of ca = 2
Number of al = 2
and so on

I was trying to get something going with ...

function myprob(input)
number_of_c= length(input(ismember(input,'c')>0))

But I couldn't figure out how to assign anything to "not a variable", Should I use a cell array? I am just looking for some guidance here. I realize I may have stated my problem clearly but thanks for any help you can give


Using "input" as a variable name is not recommended, as "input" is the name of a common MATLAB system routine.

I am trying to plot a histogram of a bit array, Lets say x = (1:1:1000) and i want to divide it into different parts such as 1-20, 21-40,.....,981-1000.

X = (1:1:1000)';
Edges = (1:20:1000)';
Count = histc(X,Edges);

This is the way i had proceed to count the bit error rate in different parts such as for example i had 10 bit error rate from 1-20 and so on.

But i am not able to find the solution.

Lalit, you should have created a new Question for this.

In the case where the length of the array is evenly dividable by 20, then:

counts = sum( reshape(YourBitArray, 20, []) );
bar(counts);   %draw the histogram


No products are associated with this question.

2 Answers

Answer by Naz
on 20 Nov 2011

For single letter detection I would create an array of 26 members. Now, assuming you have non-capital letters in the string you can just read each letter, convert it to number subtract 64 and count it as in histogram. For example, in the forloop if you get a letter 'a', that is 65 and you subtract 64, you get 1 (since 'a' is the first letter in the alphabet):

for n=1:length(string)

Obviously, the code will not run as is, but I hope the idea is clear. So, as a result your array from 1to26 will contain number of occurrence for each letter from a to z respectively.


on 20 Nov 2011

thank you. this was very helpful. I changed '64' to '96' because 'a' has a value of '97'. I'm now trying to take my function, after I've found the quantity of each letter in the input string and have it be displayed like

quantity of letter a = xxx
quantity of letter f = xxx

How about
for k = 1:26
fprintf('The count for letter %c is %d',...
k+64, histogram(k));

Note: you'll need to make this more robust if you want to handle both upper and lower case as well as other symbols, but this gives you the idea.

on 20 Nov 2011

I haven't tried this out yet. but thank you for the help. It's helping me think about how to approach more situations like this

Answer by Image Analyst
on 20 Nov 2011

Try this simple demo:

% Ask user for their character string.
prompt = {'Enter the character string:'};
dlg_title = 'Enter letters'; % Needs to be short to be completely seen
num_lines = 1; % Lines per edit field.
defaultResponses = {'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^'};
response = inputdlg(prompt, dlg_title, num_lines, defaultResponses);
response = cell2mat(response)
if isempty(response)
	% Bail out if they click Cancel.
% Get list of unique characters entered.
allChars = unique(response)
% Go through the list of unique characters
% getting the count for each one.
for c = 1 : length(allChars)
	countForThisChar = sum(response == allChars(c));
	histogram(c) = countForThisChar;
	fprintf('Counted %d occurrences of character %s\n', ...
		countForThisChar, allChars(c));
% Display letters then character counts (all 1's if default input was used)

Note: It can handle any characters the user can input. The histogram bins for elements may not be at the same element number for subsequent runs but could be trivially adapted if you need that. This gives you just the count for what's there. If you always need "a" at bin 65 and "A" at bin 97 (their ascii values) then you'll need to do that simple adaptation.

  1 Comment


This should be the ACCEPTED ANSWER. Image Analyst's code is simple and counts the number of occurrences of ANY characters and displays them. Well done!

Join the 15-year community celebration.

Play games and win prizes!

Learn more
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

MATLAB Academy

New to MATLAB?

Learn MATLAB today!