Need help with string counting

1 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2020-3-4
编辑:Stephen23 2020-3-4
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.

请先登录,再进行评论。

采纳的回答

Jamuna
Jamuna 2020-3-4
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

更多回答(2 个)

KSSV
KSSV 2020-3-4
编辑:KSSV 2020-3-4
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.

类别

Help CenterFile Exchange 中查找有关 Numeric Types 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by