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

# Finding the number of distinct letters in a string

Asked by Andrew on 19 Nov 2011

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

## 1 Comment

Walter Roberson on 20 Nov 2011

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

## Products

No products are associated with this question.

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):

```histogram=zeros(1,26);
for n=1:length(string)
currentLetter=string(n);
histogram(currentLetter-64)=histogram(currentLetter-64)+1;
end
```

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.

Andrew 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

Image Analyst on 20 Nov 2011

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

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.

Andrew 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.
return;
end
% 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));
end
% Display letters then character counts (all 1's if default input was used)
allChars
histogram
```

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.