Need help with string counting
Ältere Kommentare anzeigen
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 Kommentar
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.
Akzeptierte Antwort
Weitere Antworten (2)
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
am 4 Mär. 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.
Kategorien
Mehr zu Image Arithmetic finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!