Need help with string counting

1 view (last 30 days)
Ricardas Gudonavicius
Ricardas Gudonavicius on 4 Mar 2020
Edited: Stephen23 on 4 Mar 2020
Hello. I have problem with counting string.
First of all, I need to count how many i have letters in word (using this):
prompt='Iveskite raide/zodi-';
str=input(prompt,'s');
gauto_zodio_ilgis=length(str);
Second, I write to command window LABAS
My all arrays:
My code:
dydis=0;
prompt='Iveskite raide/zodi-';
str=input(prompt,'s');
gauto_zodio_ilgis=length(str);
% for sk=1:gauto_zodio_ilgis
if strfind(str,'A')
dydis = dydis + length(A);
end
if strfind(str,'B')
dydis = dydis + length(B);
end
if strfind(str,'C')
dydis = dydis + length(C);
end
if strfind(str,'D')
dydis = dydis + length(D);
end
if strfind(str,'E')
dydis = dydis + length(E);
end
if strfind(str,'F')
dydis = dydis + length(F);
end
if strfind(str,'G')
dydis = dydis + length(G);
end
if strfind(str,'H')
dydis = dydis + length(H);
end
if strfind(str,'I')
dydis = dydis + length(I);
end
if strfind(str,'J')
dydis = dydis + length(J);
end
if strfind(str,'K')
dydis = dydis + length(K);
end
if strfind(str,'L')
dydis = dydis + length(L);
end
if strfind(str,'M')
dydis = dydis + length(M);
end
if strfind(str,'N')
dydis = dydis + length(N);
end
if strfind(str,'O')
dydis = dydis + length(O);
end
if strfind(str,'P')
dydis = dydis + length(P);
end
if strfind(str,'Q')
dydis = dydis + length(Q);
end
if strfind(str,'R')
dydis = dydis + length(R);
end
if strfind(str,'S')
dydis = dydis + length(S);
end
if strfind(str,'T')
dydis = dydis + length(T);
end
if strfind(str,'U')
dydis = dydis + length(U);
end
if strfind(str,'V')
dydis = dydis + length(V);
end
if strfind(str,'W')
dydis = dydis + length(W);
end
if strfind(str,'X')
dydis = dydis + length(X);
end
if strfind(str,'Y')
dydis = dydis + length(Y);
end
if strfind(str,'Z')
dydis = dydis + length(Z);
end
I get answer dydis = 140000, but by my counting I have to get 165000
HOW TO I NEED TO CHANGE MY CODE???
Thanks for any help
  1 Comment
Stephen23
Stephen23 on 4 Mar 2020
Edited: Stephen23 on 4 Mar 2020
You should read this:
and then follow KSSV's advice.
Or use a table.
Or a structure.
Whatever you do, do NOT write slow, complex, buggy code using evil eval.

Sign in to comment.

Accepted Answer

Jamuna
Jamuna on 4 Mar 2020
update your code of if statements with
for ii=1:26
if strfind(str,char(ii+64))
dydis = dydis + length(eval(char(ii+64)))*numel(strfind(str,char(ii+64)));
end
end
this should give you right answer

More Answers (2)

KSSV
KSSV on 4 Mar 2020
Edited: KSSV on 4 Mar 2020
You have made your code very lengthy......you should follow like this.
S = {'A','B',.....'Z'} ; % write all the letters
dydis=0;
for i = 1:length(S)
W = strfind(str,S{i}) ;
dydis = dydis + length(W);
end

Constantino Carlos Reyes-Aldasoro
Perhaps the error is here:
if strfind(str,'A')
dydis = dydis + length(A);
end
You are detecting the character 'A' but then you are finding the length of a variable called 'A'. Try like this
A_inString = strfind(str,'A');
if ~isempty(A_inString)
dydis = dydis + length(A_inString);
end
You save the output of the find in a variable, and then if it is not empty, then you add the length of the strfind.
Hope this helps. If this solves the problem, please accept the answer. If not, let me know.

Categories

Find more on Numeric Types in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!