Need help with string counting
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Ricardas Gudonavicius
le 4 Mar 2020
Modifié(e) : Stephen23
le 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 commentaire
Réponse acceptée
Jamuna
le 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
2 commentaires
Stephen23
le 4 Mar 2020
Or write simpler and more efficient code which avoids evil eval entirely, such as that shown by KSSV here:
Plus de réponses (2)
Constantino Carlos Reyes-Aldasoro
le 4 Mar 2020
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.
Voir également
Catégories
En savoir plus sur Mathematics and Optimization dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!