Need help with string counting

조회 수: 1 (최근 30일)
Ricardas Gudonavicius
Ricardas Gudonavicius 2020년 3월 4일
편집: Stephen23 2020년 3월 4일
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
Stephen23
Stephen23 2020년 3월 4일
Or write simpler and more efficient code which avoids evil eval entirely, such as that shown by KSSV here:
Ricardas Gudonavicius
Ricardas Gudonavicius 2020년 3월 4일
THANKS

댓글을 달려면 로그인하십시오.

추가 답변 (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